Click here to Skip to main content
14,970,304 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
This is in answer to the following article: (I can't otherwise find a link to simply respond to that article)

A DateTimePicker with working BackColor
By Vincenzo Rossi

Sorry to come up with a simpler solution that has no limitations. I hit the same problem and came to the same conclusions as in this post. However I didn't like the limitations, namely that one couldn't anymore edit directly the days/months/years in the control.
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 one of your choice...)
If you want to change color, simply call the Invalidate() method of your DateTimePicker after changing _backColor.
To me that works wonderful, without any limitation.
Posted
Updated 28-Jul-11 22:56pm
v2
Comments
chris b. 3-Sep-15 16:08pm
   
Limitation is, it doesn't Work on Win 7 or above with Visual Styles enabled :-(

If this is a suggestion rather than a question, then you should post it to the Tips & Tricks section. Leaving it here it is likely to disappear from view.
   
Comments
Member 11460356 29-Jun-15 3:21am
   
In default datetimepicker as I am selecting time it turns to blue but in this one its not turning up . I need to turn it up to blue when I select a part of time in datetime picker .
Would this not work??

dateTimePicker1.CalendarMonthBackground = Color.Blue;
   
Comments
David Biggins 5-Feb-15 13:12pm
   
That would set the colour of the dropdown calendar - not of the unexpanded control on the form.
VB
No, it doesn't. I think the original article already explained that in order for it to work you must use SetStyle(UserPaint, true).
But then if you do that, you loose all the system implementation regarding the DateTimePicker... and it doesn't show much useful except your nice back color... It's one of these Microsoft not quite user friendly stuff... However we must thank them for the number of other good stuff they also provide us...)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900