Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set the background color of a DateTimePicker control.

Controls

How to set the background color of a DateTimePicker control.

by  ThatRickGuy  Posted    (Edited  )
Due to the way DateTimePicker was implimented, there is no default way to change the back color. There is a .BackColor property that overrides the Control.BackColor property, but it is empty. So, in order to have a working .BackColor property we will have to build off of the existing DateTimePicker.

If you have a general library project, that would be an excellent place to put this, if not, just start a new DLL project.

Add a Component Class to your project. Because of the joys of NameSpaces you can even call your new component DateTimePicker.

Select everything in the sub and paste this over it:
Code:
Public Class DateTimePicker
  Inherits System.Windows.Forms.DateTimePicker

#Region " Component Designer generated code "

  Public Sub New()
    MyBase.New()

    ' This call is required by the Component Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

  End Sub

  'Control overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

  'Required by the Control Designer
  Private components As System.ComponentModel.IContainer

  ' NOTE: The following procedure is required by the Component Designer
  ' It can be modified using the Component Designer.  Do not modify it
  ' using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    components = New System.ComponentModel.Container()
  End Sub

#End Region

  'The module level variable that will store the background color
  Private mBackColor As Color = SystemColors.Window

  'The BackColor property we will be calling
  Public Overrides Property BackColor() As Color
    Get
      Return mBackColor
    End Get
    Set(ByVal Value As Color)
      mBackColor = Value
      'After the BackColor has been set, Invalidate the control
      'This will force it to be redrawn
      Invalidate()
    End Set
  End Property

  'WndProc fires durring the painting of the control
  Protected Overrides Sub WndProc(ByRef m As Message)
    'Check to see if message being send is WM_ERASEBKGND. 
    'The hex value of this message is &H14.
    'This message is sent when the background of the 
    'object needs to be erased. In our case though, instead of
    'erasing it, we will paint a rectangle over it
    If m.Msg = CInt(&H14) Then ' WM_ERASEBKGND 
      Dim g As Graphics = Graphics.FromHdc(m.WParam)
      g.FillRectangle(New SolidBrush(mBackColor), ClientRectangle)
      g.Dispose()
      Return
    End If
    MyBase.WndProc(m)
  End Sub
End Class

Compile the project.

Open the project that you want to have the new DateTimePicker on.

In the Tools menu, select "Customize Toolbox".

Switch to the .Net Framework Components tab and click on "Browse"

Browse to the .DLL you just compiled from the project with the new DateTimePicker.

Order the list by namespace and look for the DateTimePicker in your .DLL.

Hit okay. The new DateTimePicker will appear on your tool box. Any project you add it to will automaticly add any references it needs and you'll be good to go.

-Rick

Special Thanks to:
Claudio Ganahl - For the Original C code
Bernard Manning - For converting the code to VB.Net
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top