Click here to Skip to main content
11,576,354 members (60,679 online)
Click here to Skip to main content

A DateTimePicker with working BackColor

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

Introduction

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.

Background

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) {
    base.OnPaint(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;
    }

    // Painting...in 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);

    g.Dispose();
    bkgBrush.Dispose();
}

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

[Browsable(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.

BCDateTimePickerProperties.gif

[Category("Appearance"), 
 Description("The background color of the component when disabled")]
[Browsable(true)]
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.

License

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

Share

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.



"Short code, good code"



You may also be interested in...

Comments and Discussions

 
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 
GeneralRe: Kool control Pin
Donsw20-Jan-09 14:31
memberDonsw20-Jan-09 14:31 
GeneralRe: Kool control Pin
Nitin Sawant10-Dec-09 22:48
memberNitin Sawant10-Dec-09 22:48 
QuestionException Thrown Pin
verdant726-Dec-08 8:29
memberverdant726-Dec-08 8:29 
AnswerException Thrown - HOW TO FIX ! Pin
Vincenzo Rossi29-Dec-08 12:48
memberVincenzo Rossi29-Dec-08 12:48 
GeneralDemo not compiling Pin
colkaih24-Nov-08 22:55
membercolkaih24-Nov-08 22:55 
AnswerRe: Demo not compiling Pin
Vincenzo Rossi27-Nov-08 10:19
memberVincenzo Rossi27-Nov-08 10:19 
GeneralThanks! Pin
tehtb3-Nov-08 23:12
membertehtb3-Nov-08 23:12 
JokeRe: Thanks! Pin
Vincenzo Rossi4-Nov-08 5:14
memberVincenzo Rossi4-Nov-08 5:14 
GeneralVery nice solution Pin
Mark.Whitfeld3-Nov-08 21:44
memberMark.Whitfeld3-Nov-08 21:44 
GeneralRe: Very nice solution Pin
Vincenzo Rossi4-Nov-08 6:21
memberVincenzo Rossi4-Nov-08 6:21 

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

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

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