Option Strict On

Imports system
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel

Public Class extDateTimePicker
    Inherits System.Windows.Forms.DateTimePicker

    ' Color used for background and foreground when control is disabled
    Private _BackColorDisabled As Color = System.Drawing.SystemColors.Control
    Private _ForeColorDisabled As Color = Color.Black

    'HOLDS ALIGNMENT FOR STRING TO BE DRAWN
    Private _StringFormatAlignment As New StringFormat

    'Font used for control when painted
    Private _DisabledFont As Font = New System.Drawing.Font("Times New Roman", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

    'BRUSH USED TO PAINT TEXT
    Private _TextBrush As New SolidBrush(_ForeColorDisabled)
    'BRUSH USED TO PAINT BACKCOLOR
    Private _BackBrush As New SolidBrush(_BackColorDisabled)

    <Category("Appearance"), _
    Description("Sets the BackColor to Non-Standard when Disabled"), _
    DefaultValue(GetType(Color), "Light Yellow")> _
    Public Property BackColorDisabled() As Color
        Get
            Return _BackColorDisabled
        End Get
        Set(ByVal Value As Color)
            _BackColorDisabled = Value
            _BackBrush.Color = Value
        End Set
    End Property

    <Category("Appearance"), _
    Description("Sets the BackColor to Non-Standard when Disabled"), _
    DefaultValue(GetType(Color), "Black")> _
    Public Property ForeColorDisabled() As Color
        Get
            Return _ForeColorDisabled
        End Get
        Set(ByVal Value As Color)
            _ForeColorDisabled = Value
            _TextBrush.Color = Value
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        'if not enabled, paint custom, otherwise just call
        ' base class OnPaint so control will do it automatically
        Select Case Me.Enabled
            Case True 'control enabled let paint automatically
                MyBase.OnPaint(e)
            Case False 'control disabled paint manually (maybe)
                Select Case Me.ShowCheckBox
                    Case True 'check box shown
                        Select Case Me.Checked
                            Case True 'check box checked...has data
                                e.Graphics.FillRectangle(_BackBrush, ClientRectangle)
                                e.Graphics.DrawString(Me.Text, _DisabledFont, _TextBrush, e.ClipRectangle, _StringFormatAlignment)
                            Case False 'check box not checked...no data
                                e.Graphics.FillRectangle(_BackBrush, ClientRectangle)
                        End Select
                    Case False
                        e.Graphics.FillRectangle(_BackBrush, ClientRectangle)
                        e.Graphics.DrawString(Me.Text, _DisabledFont, _TextBrush, e.ClipRectangle, _StringFormatAlignment)
                End Select
        End Select
    End Sub

    Protected Overrides Sub OnEnabledChanged(ByVal e As System.EventArgs)

        MyBase.OnEnabledChanged(e)

        If Not Me.Enabled Then
            Me.SetStyle(ControlStyles.UserPaint, True)
        Else
            Me.SetStyle(ControlStyles.UserPaint, False)
            'setting the font again here fixes bug 
            'where font looks bold even though its not
            '**this only works if the font is the default font**
            Me.Font = _DisabledFont
            'invalidate control so it repaints. 
            'If you don't sometimes the text is 
            'invisible until the control gets focus
            Me.Invalidate()
        End If
    End Sub
End Class