Link to home
Start Free TrialLog in
Avatar of ianinspain
ianinspain

asked on

Disable a textbox (winforms) but without changing colors?

Hi there,

Can anyone suggest a way to disable textboxes and stop them getting focus without changing the colours..

I have tried putting the textboxes in panels and groupboxes ... and then disabling the group/panel.. and it does work but the textboxes automatically change colors to look like they are dimmed... and i want to stop this...

Can anyone help?

Thanks in advance

Ian
Avatar of BlackTigerX
BlackTigerX

make it readOnly
From http://www.windowsforms.net/FAQs/default.aspx?PageID=3&CategoryID=3&SubcategoryID=78:


How do I specify the text color of a read-only or disabled TextBox?  
 
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?

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.
  SetStyle( ControlStyles.UserPaint, true );
  InitializeComponent();
}


Make the TextBox'es ReadOnly AND set the BackColor to the color you want to see
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial