.netfuscation

Thursday, February 24, 2005

Overriding gray text in TextBox

Overriding the graytext color in a TextBox could'nt be easier.


protected void FixupColors()
{
if (Enabled)
{
this.BackColor = System.Drawing.SystemColors.Window;
}
else
{
this.BackColor = System.Drawing.SystemColors.Control;
}


this.SetStyle(ControlStyles.DoubleBuffer, !Enabled);
this.SetStyle(ControlStyles.UserPaint, !Enabled);
this.RecreateHandle();
}



protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
e.Graphics.DrawString(this.Text, Font, SystemBrushes.WindowText, this.ClientRectangle);
}



protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged (e);
FixupColors();
}