Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A simple solution for changing a DateTimerPicker background color

0.00/5 (No votes)
29 Jul 2011 1  
How to change the background color of a DateTimerPicker control.

This is an answer to a question in comments section of the following article (I can't otherwise find a link to simply respond to that question): A DateTimePicker with working BackColor, by Vincenzo Rossi.


I wanted to come up with a simpler solution that has no limitations. I hit the same problem and came to the same conclusions as in the original post. However I didn't like the limitations, namely that one couldn't directly edit the days/months/years in the control anymore. Then I found a great solution here:
http://stackoverflow.com/questions/198532/changing-the-background-color-of-a-datetimepicker-in-net.


Basically, you create a control inheriting from DateTimePicker and add this override:


C#
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);
}

(where _backColor is the color of your choice...)


If you want to change the color, simply call the Invalidate() method of your DateTimePicker after changing _backColor. To me, that works wonderful, without any limitations.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here