Changing the background color of a DateTimePicker in .NET
According to MSDN :
Setting the
BackColor
has no effect on the appearance of theDateTimePicker
.
You need to write a custom control that extends DateTimePicker
. Override the BackColor
property and the WndProc
method.
Whenever you change the BackColor
, don't forget to call the myDTPicker.Invalidate()
method. This will force the control to redrawn using the new color specified.
const int WM_ERASEBKGND = 0x14;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg == WM_ERASEBKGND)
{
using(var g = Graphics.FromHdc(m.WParam))
{
using(var b = new SolidBrush(_backColor))
{
g.FillRectangle(b, ClientRectangle);
}
}
return;
}
base.WndProc(ref m);
}
There is a free implementation derived from DateTimePicker
that allows you to change BackColor
property on change.
See the CodeProject website: DateTimePicker
with working BackColor