Click here to Skip to main content
13,454,676 members (56,595 online)
Click here to Skip to main content
Add your own
alternative version


56 bookmarked
Posted 3 Nov 2008

A DateTimePicker with working BackColor

, 3 Nov 2008
Rate this:
Please Sign up or sign in to vote.
A DateTimePicker derived class showing the background color we want.


Some Windows Forms controls in .NET don't allow us to change the BackColor. This is true in the case of DateTimePicker. Another known problem is the poor readability of the text when the controls are disabled. In this example, I have derived such a class introducing the ability to set BackColor, and I've added a new property BackDisabledColor.


The BackColor property is defined in the base class Control, and therefore is available in the derived classes. Also, such a property is usually available in the Properties window at design-time. However, the standard DateTimePicker is not able to draw a user chosen background color. If we set it, nothing happens. So, in the DateTimePicker class, this property is overridden with the attribute [Browsable(false)]. In this way, we don't have to see a useless property.

The DateTimePicker control uses some Windows features to draw the control. So, the WM_PAINT message is not managed by OnPaint method of the class itself. The OnPaint method is never called. To force calling that method in our derived class, we have to set the control style to UserPaint in the constructor:

this.SetStyle(ControlStyles.UserPaint, true);

Now, we can use our OnPaint, but we cannot take advantage of the base OnPaint because it does nothing. To check this, try the following code:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {

You will see the control showing nothing. So now, we have the entire responsibility of drawing the control. In order to keep a useful and clear code, we abandon two original features otherwise requiring a lot of coding. The first one is the optional checkbox used to allow end-users to skip the displayed date (the ShowCheckBox property). The second is the ability to enter date parts directly using keyboard and not through a calendar.

Using the Code

To use the code, simply add the DateTimePicker.cs file to your project and use the BCDateTimePicker just like its base class.

The main work is done by an override of the OnPaint method. Here, we put the code to draw the background rectangle, filled with the color we want, and then draw the drop down button and the text representing the date. The work of drawing the drop down button is done by ComboBoxRenderer.DrawDropDownButton.

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    Graphics g = this.CreateGraphics();

    //The dropDownRectangle defines position and size of dropdownbutton block, 
    //the width is fixed to 17 and height to 16. 
    //The dropdownbutton is aligned to right
    Rectangle dropDownRectangle = 
       new Rectangle(ClientRectangle.Width - 17, 0, 17, 16);
    Brush bkgBrush;
    ComboBoxState visualState;

    //When the control is enabled the brush is set to Backcolor, 
    //otherwise to color stored in _backDisabledColor
    if (this.Enabled) {
         bkgBrush = new SolidBrush(this.BackColor);
         visualState = ComboBoxState.Normal;
    else {
        bkgBrush = new SolidBrush(this._backDisabledColor);
        visualState = ComboBoxState.Disabled;

    // action

    //Filling the background
    g.FillRectangle(bkgBrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);
    //Drawing the datetime text
    g.DrawString(this.Text, this.Font, Brushes.Black, 0, 2);

    //Drawing the dropdownbutton using ComboBoxRenderer
    ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, visualState);


In order to show the BackColor property in the Properties window again, we override it, setting the Browsable attribute to true.

public override Color BackColor
    get { return base.BackColor; }
    set { base.BackColor = value; }

Also, we create a BackDisabledColor property allowing the developer to choose the color at design-time.


 Description("The background color of the component when disabled")]
public Color BackDisabledColor
    get { return _backDisabledColor; }
    set { _backDisabledColor = value; }

Points of Interest

In the overridden OnPaint method, I've created a new Graphics object from the control rather than use the one returned by PaintEventArgs. In fact, when using the ShowUpDown mode, the base class invalidates only the date part region involved by user interaction. When using the long format, this produces an incorrect text display. Since I've found no other way to extend the clipping region of the Graphics object passed by the event argument, the solution adopted is to create a new Graphics, allowing to draw in the whole extent of the control.


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


About the Author

Vincenzo Rossi
Software Developer (Senior)
Italy Italy
I'm a graduate in Computer Science.
I work with C++, Visual Basic 6, C#, asp, asp.Net, Windows Forms, SQL Server, Access, Flash.

I also like writing songs and playing around with my band Diversamente Rossi.
This is the video of the song Vivo così from the album L'immobile disegno (The immobile drawing).

"Short code, good code"

You may also be interested in...

Comments and Discussions

QuestionLo usé Pin
Member 1099647629-Mar-17 12:47
memberMember 1099647629-Mar-17 12:47 
QuestionComboboxRenderer Error - Visual Styles-related operation resulted in an error because no visual style is currently active. Pin
Member 1002482321-Nov-14 5:40
memberMember 1002482321-Nov-14 5:40 
AnswerCode in VB.Net Pin
SumitSaha17-Sep-14 15:22
memberSumitSaha17-Sep-14 15:22 
AnswerCode in VB.Net Pin
SumitSaha17-Sep-14 15:19
memberSumitSaha17-Sep-14 15:19 
QuestionA DateTimePicker with working BackColor Pin
Member 1015114211-Jul-13 4:13
memberMember 1015114211-Jul-13 4:13 
AnswerRe: A DateTimePicker with working BackColor Pin
SeriketZu28-Jul-13 17:58
memberSeriketZu28-Jul-13 17:58 
Bugit not work in windows xp or classical mode Pin
thangto12-Feb-13 15:25
memberthangto12-Feb-13 15:25 
GeneralRe: it not work in windows xp or classical mode Pin
thangto13-Feb-13 16:52
memberthangto13-Feb-13 16:52 
GeneralMy vote of 4 Pin
JHankins24-May-12 13:29
memberJHankins24-May-12 13:29 
QuestionText Selection? Pin
baer99915-Dec-11 11:03
memberbaer99915-Dec-11 11:03 
GeneralMy vote of 5 Pin
Giuseppe834-Aug-11 3:14
memberGiuseppe834-Aug-11 3:14 
GeneralC++ Pin
Gyulchin8-Apr-11 2:22
memberGyulchin8-Apr-11 2:22 
GeneralMy vote of 5 Pin
talisker102-Feb-11 23:44
membertalisker102-Feb-11 23:44 
GeneralMy vote of 5 Pin
ClosedText4-Jan-11 5:53
memberClosedText4-Jan-11 5:53 
GeneralMy vote of 5 Pin
malcomm19-Oct-10 15:23
membermalcomm19-Oct-10 15:23 
GeneralMy vote of 2 Pin
Member 166679415-Oct-10 2:02
memberMember 166679415-Oct-10 2:02 
GeneralMy vote of 5 Pin
Mendocina3-Aug-10 23:34
memberMendocina3-Aug-10 23:34 
GeneralReally good example, but... Pin
lysergid14-Jun-10 20:32
memberlysergid14-Jun-10 20:32 
GeneralTab stop doesn't work Pin
neriman2311-Mar-10 23:46
memberneriman2311-Mar-10 23:46 
QuestionCool. What if I want to set the border of the control? Pin
luisfleitas18-May-09 14:15
memberluisfleitas18-May-09 14:15 
AnswerRe: Cool. What if I want to set the border of the control? Pin
luisfleitas18-May-09 15:01
memberluisfleitas18-May-09 15:01 
GeneralRe: Cool. What if I want to set the border of the control? Pin
baer99925-May-10 23:09
memberbaer99925-May-10 23:09 
GeneralRe: Cool. What if I want to set the border of the control? Pin
dave.toland7-Mar-12 1:07
memberdave.toland7-Mar-12 1:07 
GeneralKool control Pin
Donsw20-Jan-09 5:56
memberDonsw20-Jan-09 5:56 
GeneralRe: Kool control Pin
Vincenzo Rossi20-Jan-09 10:34
memberVincenzo Rossi20-Jan-09 10:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180321.1 | Last Updated 3 Nov 2008
Article Copyright 2008 by Vincenzo Rossi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid