Setting a DateTimePicker Control’s Background Color
The datetimepicker control contained in the .NET Framework 1.0 and 1.1 does not support setting its background color. The class below inherits from System.Windows.Forms.DateTimePicker
and extends it by a working BackColor
property:
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Public Class ExtendedDateTimePicker
Inherits DateTimePicker
Private m_BackBrush As SolidBrush
< _
Browsable(True), _
DesignerSerializationVisibility( _
DesignerSerializationVisibility.Visible _
) _
> _
Public Overrides Property BackColor() As Color
Get
Return MyBase.BackColor
End Get
Set(ByVal Value As Color)
If Not m_BackBrush Is Nothing Then
m_BackBrush.Dispose()
End If
MyBase.BackColor = Value
m_BackBrush = New SolidBrush(Me.BackColor)
Me.Invalidate()
End Set
End Property
Protected Overrides Sub WndProc(ByRef m As Message)
Const WM_ERASEBKGND As Int32 = &H14
If m.Msg = WM_ERASEBKGND Then
Using g As Graphics = Graphics.FromHdc(m.WParam)
If m_BackBrush Is Nothing Then
m_BackBrush = New SolidBrush(Me.BackColor)
End If
g.FillRectangle(m_BackBrush, Me.ClientRectangle)
End Using
Else
MyBase.WndProc(m)
End If
End Sub
Protected Overloads Overrides Sub Dispose( _
ByVal disposing As Boolean _
)
If disposing AndAlso Not m_BackBrush Is Nothing Then
m_BackBrush.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
End Class