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
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.
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;
}
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;
}