How to change the font color of a disabled TextBox?

后端 未结 9 681
被撕碎了的回忆
被撕碎了的回忆 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.

提交回复
热议问题