1

Is it possible to remove the border of DateTimePicker?
Because I'm trying to make it border-less for design.

Jimi
  • 29,621
  • 8
  • 43
  • 61

1 Answers1

4

An example using a Custom Control, derived from DateTimePicker.
It adds a few public properties that allow to change the Color and style of the Control's Border.

  • BorderColor sets the Color of the Border. It defaults to Color.Transparent.
  • BorderStyle sets the Border Style (Solid, Dotted, Dashed etc.). It defaults to Solid.
  • BorderColorUseBackColor is a switch, used to set the BorderColor Property to the Control's BackColor value. When set to True, the BorderColor cannot be changed. The value set is however cached and will be set when the Property is set to False.

To draw the Border as defined by these properties, the Control overrides WndProc to handle the WM_PAINT message and draws a custom Border using the standard ControlPaint.DrawBorder() method.

If you want to hide the Control's Border, you can just set BorderColorUseBackColor = True (as described, the BorderColor is fixed to the Control's BackColor, hence it's not visible).

Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

<DesignerCategory("Code"), ToolboxItem(True)>
Public Class DateTimePickerEx
    Inherits DateTimePicker

    Const WM_PAINT As Integer = &HF

    Private m_BorderStyle As ButtonBorderStyle = ButtonBorderStyle.Solid
    Private m_BorderColor As Color = Color.Transparent
    Private m_CurrentBorderColor As Color = Color.Transparent
    Private m_UseBackColor As Boolean = False

    <DefaultValue(GetType(Color), "Transparent")>
    Public Property BorderColor As Color
        Get
            Return m_BorderColor
        End Get
        Set
            If m_UseBackColor Then
                m_CurrentBorderColor = Value
                Return
            End If
            If m_BorderColor <> Value Then
                m_BorderColor = Value
                Invalidate()
            End If
        End Set
    End Property

    <DefaultValue(GetType(ButtonBorderStyle), "Solid")>
    Public Property BorderStyle As ButtonBorderStyle
        Get
            Return m_BorderStyle
        End Get
        Set
            If m_BorderStyle <> Value Then
                m_BorderStyle = Value
                Invalidate()
            End If
        End Set
    End Property

    <DefaultValue(GetType(Boolean), "False")>
    Public Property BorderColorUseBackColor As Boolean
        Get
            Return m_UseBackColor
        End Get
        Set
            If Value Then
                m_CurrentBorderColor = m_BorderColor
                BorderColor = BackColor
                m_UseBackColor = Value
            Else
                m_UseBackColor = Value
                BorderColor = m_CurrentBorderColor
            End If
        End Set
    End Property

    Protected Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)

        Select Case m.Msg
            Case WM_PAINT
                If IsHandleCreated Then
                    Using g = Graphics.FromHwndInternal(Me.Handle)
                        ControlPaint.DrawBorder(g, ClientRectangle, m_BorderColor, m_BorderStyle)
                    End Using
                    m.Result = IntPtr.Zero
                End If
        End Select
    End Sub
End Class
Jimi
  • 29,621
  • 8
  • 43
  • 61