tags:

views:

3683

answers:

3

In .NET (at least in the 2008 version, and maybe in 2005 as well), changing the BackColor property of a DateTimePicker has absolutely no affect on the appearance. How do I change the background color of the text area, not of the drop-down calendar?

Edit: I was talking about Windows forms, not ASP.

A: 

for the text area you would have do it on the actual text box that you are adding the extender onto
for c# you would do it like this:

<asp:TextBox runat="server" ID="txt" BackColor="Aqua" Text="Date"></asp:TextBox>
jmein
I'm sorry it took so long to get to this. I thought I had already added this comment. I was talking about windows applications, not ASP applications.
Daniel
+4  A: 

According to MSDN :

Setting the BackColor has no effect on the appearance of the DateTimePicker.

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)
     {
       Graphics g = Graphics.FromHdc(m.WParam);
       g.FillRectangle(new SolidBrush(_backColor), ClientRectangle);
       g.Dispose();
       return;
     }

     base.WndProc(ref m);
}
Vivek
Sorry it took so long to accept. I was hoping there were other solutions that didn't involve knowing hex values, then I forgot about this.
Daniel
Thank you for this post. I am custom drawing a DateTimePicker control and this put me in the correct direction.
Chris Porter
I tried this change under Vista (with and without visual styles) and it does not work.
Phil Wright
Same experience as Phil Wright running Win 7
Qua
@Qua did you try calling myDTPicker.Invalidate() after setting the background color?
Vivek
+1  A: 

There is a free implementation derived from DateTimePicker that allows you to change BackColor property on change CodeProject website at DateTimePicker with working BackColor

Gustavo