Is it possible to remove the border of DateTimePicker?
Because I'm trying to make it border-less for design.
Asked
Active
Viewed 751 times
1

Jimi
- 29,621
- 8
- 43
- 61

Jess Tremblay
- 45
- 7
-
What are you using Windows Forms, WPF or ? – Mark Hall Mar 03 '21 at 02:23
-
Windows Form Bro – Jess Tremblay Mar 03 '21 at 02:45
-
1In addition to Jimi's answer, you may want to take a look at this [Flat DateTimePicker](https://stackoverflow.com/a/66092509/3110834). You can download/clone the project and add it to your solution. – Reza Aghaei Mar 03 '21 at 06:49
1 Answers
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 toColor.Transparent
.BorderStyle
sets the Border Style (Solid, Dotted, Dashed etc.). It defaults toSolid
.BorderColorUseBackColor
is a switch, used to set theBorderColor
Property to the Control'sBackColor
value. When set toTrue
, theBorderColor
cannot be changed. The value set is however cached and will be set when the Property is set toFalse
.
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