Search code examples
c#textbox

Change a textbox colour when disabled C#


i am going to ask the same thing like other topic which I have seen.

When i use the Textbox.Enable or Textbox.readOnly, the textbox gets a dark colour, how can i change this colour for a better colour? (white could be better).


Solution

  • When a TextBox is disabled, it ignores the ForeColor. You can override this by implementing your custom painting.

    From the source Thread:-

    You can override the OnPaint event like something like this:-

    protected override void OnPaint(PaintEventArgs e)
    {
         SolidBrush drawBrush = new SolidBrush(ForeColor);
         // Draw string to screen.
         e.Graphics.DrawString(Text, Font, drawBrush, 0f,0f); 
    }
    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
    }