How to change the font color of a disabled TextBox?

后端 未结 9 590
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 05:13

Does anyone know which property sets the text color for disabled control? I have to display some text in a disabled TextBox and I want to set its color to blac

相关标签:
9条回答
  • 2020-11-27 06:11

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

        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"

    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

    In Enter event set the focus

    int index=this.Controls.IndexOf(this.textBox1);
    
    this.Controls[index-1].Focus();
    

    So your control will not focussed and behave like disabled.

    0 讨论(0)
  • 2020-11-27 06:11

    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;
    }
    
    0 讨论(0)
  • 2020-11-27 06:18

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

    Alternatively, you can also do something like this:

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

    or

    private void FormFoo_Load(...) {
        txtFoo.SelectionLength = 0;
    }
    
    0 讨论(0)
提交回复
热议问题