change color of disabled textbox text

  • Thread starter Thread starter parez
  • Start date Start date
Hi parez,

I'm afraid you can't. The TextBox is a bit tricky and this is one of the
things it does not expose for change. The same goes for ReadOnly, but if you
only want the text in black instead of gray, using ReadOnly instead of
disabled is an option.

If you want a completely different text color your best bet would probably
be to add a transparent panel and put it over the TextBox. The default
transparancy is completely transparent but you won't be able to select the
TextBox by clicking. You would need to handle the Focus event and move the
focus elsewhere, or the user could TAB to the TextBox and edit the content.

textBox1.ForeColor = Color.Red;
textBox1.BackColor = SystemColors.Control;

TransparentPanel panel = new TransparentPanel();
panel.Location = textBox1.Location;
panel.Size = textBox1.Size;
Controls.Add(panel);
panel.BringToFront();


public class TransparentPanel : Panel
{
private int _alfa;

public int Alfa
{
get { return _alfa; }
set { _alfa = value; }
}

public TransparentPanel()
{
SetStyle(ControlStyles.Opaque, true);
}

protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.ExStyle |= 0x00000020;
return createParams;
}
}

protected override void OnPaint(PaintEventArgs e)
{
using (SolidBrush brush = new
SolidBrush(Color.FromArgb(Alfa, BackColor)))
{
e.Graphics.FillRectangle(brush, ClientRectangle);
}
}
}
 
You probably need to handle the WM_CTLCOLORSTATIC notification.

Consider using a read only textbox instead. That allows you to change both
the background and the text color

/claes
 
Hi parez,

I'm afraid you can't. The TextBox is a bit tricky and this is one of the
things it does not expose for change. The same goes for ReadOnly, but if you
only want the text in black instead of gray, using ReadOnly instead of
disabled is an option.

If you want a completely different text color your best bet would probably
be to add a transparent panel and put it over the TextBox. The default
transparancy is completely transparent but you won't be able to select the
TextBox by clicking. You would need to handle the Focus event and move the
focus elsewhere, or the user could TAB to the TextBox and edit the content.

textBox1.ForeColor = Color.Red;
textBox1.BackColor = SystemColors.Control;

TransparentPanel panel = new TransparentPanel();
panel.Location = textBox1.Location;
panel.Size = textBox1.Size;
Controls.Add(panel);
panel.BringToFront();

public class TransparentPanel : Panel
{
private int _alfa;

public int Alfa
{
get { return _alfa; }
set { _alfa = value; }
}

public TransparentPanel()
{
SetStyle(ControlStyles.Opaque, true);
}

protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.ExStyle |= 0x00000020;
return createParams;
}
}

protected override void OnPaint(PaintEventArgs e)
{
using (SolidBrush brush = new
SolidBrush(Color.FromArgb(Alfa, BackColor)))
{
e.Graphics.FillRectangle(brush, ClientRectangle);
}
}
}

--
Happy Coding!
Morten Wennevik [C# MVP]

parez said:
how do i change color of disabled textbox text?

Thanks..
I need black for now.. so I think I will change it to read only. but
is there any of making sure that that the cursor does not show up?
Should i handle the focus event and do someting?
 
Back
Top