1. Herfried K. Wagner’s VB.Any
  2. .NET
  3. Häufig gestellte Fragen

Erweitern des DateTimePicker-Steuerelements um eine Eigenschaft für dessen Hintergrundfarbe

Erweitern des DateTimePicker-Steuerelements um eine Eigenschaft für dessen
Hintergrundfarbe
<URL:https://dotnet.currifex.org/dotnet/faqs/datetimepickerbackcolor/de/>
----------------------------------------------------------------------------

Providing support for setting a datetimepicker control's background color

The DateTimePicker control contained in the .NET Framework 1.0/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
            Dim 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)
            g.Dispose()
        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
///