C# 如何更改禁用的 TextBox 的字体颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/276179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:10:37  来源:igfitidea点击:

How to change the font color of a disabled TextBox?

c#winforms

提问by Niko Gamulin

Does anyone know which property sets the text color for disabled control? I have to display some text in a disabled TextBoxand I want to set its color to black.

有谁知道哪个属性设置了禁用控件的文本颜色?我必须在禁用时显示一些文本TextBox,我想将其颜色设置为黑色。

采纳答案by Eric Schoonover

NOTE:see Cheetah's answer below as it identifies a prerequisite to get this solution to work. Setting the BackColorof the TextBox.

注意:请参阅下面的 Cheetah 答案,因为它确定了使此解决方案起作用的先决条件。设置BackColorTextBox



I think what you really want to do is enable the TextBoxand set the ReadOnlyproperty to true.

我认为您真正想做的是启用TextBox并将ReadOnly属性设置为true.

It's a bit tricky to change the color of the text in a disabled TextBox. I think you'd probably have to subclass and override the OnPaintevent.

在 disabled 中更改文本颜色有点棘手TextBox。我认为您可能必须对OnPaint事件进行子类化和覆盖。

ReadOnlythough should give you the same result as !Enabledand allow you to maintain control of the color and formatting of the TextBox. I think it will also still support selecting and copying text from the TextBoxwhich is not possible with a disabled TextBox.

ReadOnly虽然应该给你相同的结果,!Enabled并允许你保持对TextBox. 我认为它还将支持从TextBox禁用TextBox.

Another simple alternative is to use a Labelinstead of a TextBox.

另一个简单的替代方法是使用 aLabel而不是 a TextBox

回答by Cheetah

Additionally, in order for ForeColor to be obeyed on a TextBox marked ReadOnly, you must explicitly set the BackColor. If you want to have it still use the default BackColor, you have to make the set explicit, as the designer is too smart for its own good here. It is sufficient to set the BackColor to its current value. I do this in the Load event for the form, like so:

此外,为了在标记为 ReadOnly 的 TextBox 上遵循 ForeColor,您必须显式设置 BackColor。如果你想让它仍然使用默认的 BackColor,你必须明确设置,因为设计者在这里太聪明了。将 BackColor 设置为其当前值就足够了。我在表单的 Load 事件中执行此操作,如下所示:

private void FormFoo_Load(...) {
    txtFoo.BackColor = txtFoo.BackColor;
}

回答by syed qaiser

hi set the readonly attribute to true from the code side or run time not from the design time

嗨,从代码端或运行时而不是从设计时将 readonly 属性设置为 true

txtFingerPrints.BackColor = System.Drawing.SystemColors.Info;
txtFingerPrints.ReadOnly = true;

回答by Zain Ali

You can try this. Override the OnPaint event of the TextBox.

你可以试试这个。覆盖 TextBox 的 OnPaint 事件。

    protected override void OnPaint(PaintEventArgs e)
{
     SolidBrush drawBrush = new SolidBrush(ForeColor); //Use the ForeColor property
     // Draw string to screen.
     e.Graphics.DrawString(Text, Font, drawBrush, 0f,0f); //Use the Font property
}

set the ControlStyles to "UserPaint"

将 ControlStyles 设置为“UserPaint”

public MyTextBox()//constructor
{
     // This call is required by the Windows.Forms Form Designer.
     this.SetStyle(ControlStyles.UserPaint,true);

     InitializeComponent();

     // TODO: Add any initialization after the InitForm call
}

Refrence

参考

Or you can try this hack

或者你可以试试这个 hack

In Enter event set the focus

在 Enter 事件中设置焦点

int index=this.Controls.IndexOf(this.textBox1);

this.Controls[index-1].Focus();

So your control will not focussed and behave like disabled.

因此,您的控件将不会集中并表现得像禁用。

回答by karl f

If you want to display text that cannot be edited or selected you can simply use a label

如果您想显示无法编辑或选择的文本,您可以简单地使用标签

回答by Johnie Karr

In addition to the answer by @spoon16 and @Cheetah, I always set the tabstopproperty to False on the textbox to prevent the text from being selected by default.

除了@spoon16 和@Cheetah 的回答之外,我总是将tabstop文本框上的属性设置为 False,以防止默认情况下选择文本。

Alternatively, you can also do something like this:

或者,您也可以执行以下操作:

private void FormFoo_Load(...) {
    txtFoo.Select(0, 0);
}

or

或者

private void FormFoo_Load(...) {
    txtFoo.SelectionLength = 0;
}

回答by edoedoedo

I've just found a great way of doing that. In my example I'm using a RichTextBox but it should work with any Control:

我刚刚找到了一个很好的方法来做到这一点。在我的示例中,我使用的是 RichTextBox,但它应该适用于任何控件:

public class DisabledRichTextBox : System.Windows.Forms.RichTextBox
{
    // See: http://wiki.winehq.org/List_Of_Windows_Messages

    private const int WM_SETFOCUS   = 0x07;
    private const int WM_ENABLE     = 0x0A;
    private const int WM_SETCURSOR  = 0x20;

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        if (!(m.Msg == WM_SETFOCUS || m.Msg == WM_ENABLE || m.Msg == WM_SETCURSOR))
            base.WndProc(ref m);
    }
}

You can safely set Enabled = true and ReadOnly = false, and it will act like a label, preventing focus, user input, cursor change, without being actually disabled.

您可以安全地设置 Enabled = true 和 ReadOnly = false,它就像一个标签,防止焦点、用户输入、光标更改,而不会被实际禁用。

See if it works for you. Greetings

看看它是否适合你。你好

回答by Mahmoud Salah

Just handle Enable changed and set it to the color you need

只需处理启用更改并将其设置为您需要的颜色

private void TextBoxName_EnabledChanged(System.Object sender, System.EventArgs e)
{
    ((TextBox)sender).ForeColor = Color.Black;
}

回答by IT_Boy

Setting the 'Read Only' as 'True' is the easiest method.

将“只读”设置为“真”是最简单的方法。