Live Chat Icon For mobile
Live Chat Icon

When I set a TextBox to Readonly or set Enabled to false, the text color is gray. How can I force it to be the color specified in the ForeColor property of the TextBox.

Platform: WinForms| Category: TextBox

Felix Wu gives this solution in the microsoft.public.dotnet.frameworks.windowsforms newgroup.

You can download a VB.NET sample.

Override the OnPaint event of the TextBox. For example:

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
}

Note: You need to set the ControlStyles to ”UserPaint” in the constructor.

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

  InitializeComponent();

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

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.