Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ


How to set the background color of a DateTimePicker control. by ThatRickGuy
Posted: 7 Dec 04

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:


Public Class DateTimePicker
  Inherits System.Windows.Forms.DateTimePicker

#Region " Component Designer generated code "

  Public Sub New()

    ' This call is required by the Component Designer.

    '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
      End If
    End If
  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
      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
    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)
    End If
  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.


Special Thanks to:
Claudio Ganahl - For the Original C code
Bernard Manning - For converting the code to VB.Net

Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close