Custom DateTimePicker - Custom Controls WinForm C # - RJ Code Advance
Custom DateTimePicker - Custom Controls WinForm C # - RJ Code Advance
PC Helpsoft
https://rjcodeadvance.com/custom-datetimepicker-custom-controls-winform-c/ 1/12
7/22/2021 Custom DateTimePicker - Custom controls WinForm C # - RJ Code Advance
Hi :), this time we will make a custom DateTimePicker, as most of you know, this is a native
Windows control and it doesn't have a lot of appearance customization options, limiting
ourselves to just one style and layout. So today I will teach you to break those limits.
DOWNLOAD ICONS
https://rjcodeadvance.com/custom-datetimepicker-custom-controls-winform-c/ 2/12
7/22/2021 Custom DateTimePicker - Custom controls WinForm C # - RJ Code Advance
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
https://rjcodeadvance.com/custom-datetimepicker-custom-controls-winform-c/ 3/12
7/22/2021 Custom DateTimePicker - Custom controls WinForm C # - RJ Code Advance
// Fields
// -> Appearance
private Color skinColor = Color. MediumSlateBlue ;
private Color textColor = Color. White ;
private Color borderColor = Color. PaleVioletRed ;
private int borderSize = 0 ;
// Properties
public Color SkinColor
{
get { return skinColor; }
set
{
skinColor = value ;
if ( skinColor. GetBrightness () > = 0.8 F )
calendarIcon = Properties. Resources . calendarDark ;
else calendarIcon = Properties. Resources . calendarWhite ;
this . Invalidate () ;
}
}
textColor = value ;
this . Invalidate () ;
}
}
7.- Builder
In the constructor, we specify the style and behavior of the control. In this case, the control
will be painted by the user and not by the operating system. We also set the minimum
size of the control, in this way we can change the height of the DateTimePicker , you
can also do it later from the properties box.
//Builder
public RJDatePicker ()
{
this . SetStyle ( ControlStyles. UserPaint , true ) ;
this . MinimumSize = new Size ( 0 , 35 ) ;
this . Font = new Font ( this . Font . Name , 9.5 F ) ;
}
when over the icon button of the DateTimePicker, since it is not possible to tell if the pointer
It is above the dropdown icon, because the size of the DateTimePicker icon button varies
depending on the width of the control.
// Overridden methods
protected override void OnDropDown ( EventArgs eventargs )
{
base . OnDropDown ( eventargs ) ;
droppedDown = true ;
}
protected override void OnCloseUp ( EventArgs eventargs )
{
base . OnCloseUp ( eventargs ) ;
droppedDown = false ;
}
protected override void OnKeyPress ( KeyPressEventArgs e )
{
base . OnKeyPress ( e ) ;
and. Handled = true ;
}
protected override void OnPaint ( PaintEventArgs e )
{
using ( Graphics graphics = this . CreateGraphics ())
using ( Pen penBorder = new Pen ( borderColor, borderSize ))
using ( SolidBrush skinBrush = new SolidBrush ( skinColor ))
using ( SolidBrush openIconBrush = new SolidBrush ( Color. FromArgb ( 50
using ( SolidBrush textBrush = new SolidBrush ( textColor ))
using ( StringFormat textFormat = new StringFormat ())
{
RectangleF clientArea = new RectangleF ( 0 , 0 , this . Width - 0.5 F
RectangleF iconArea = new RectangleF ( clientArea. Width - calendarIc
penBorder. Alignment = PenAlignment. Inset ;
textFormat. LineAlignment = StringAlignment. Center ;
// Draw surface
graphics. FillRectangle ( skinBrush, clientArea ) ;
// Draw text
graphics. DrawString ( "" + this . Text , this . Font , textBrush, cl
// Draw open calendar icon highlight
if ( droppedDown == true ) graphics. FillRectangle ( openIconBrush, i
https://rjcodeadvance.com/custom-datetimepicker-custom-controls-winform-c/ 6/12
7/22/2021 Custom DateTimePicker - Custom controls WinForm C # - RJ Code Advance
// Draw border
if ( borderSize > = 1 ) graphics. DrawRectangle ( penBorder, clientAr
// Draw icon
graphics. DrawImage ( calendarIcon, this . Width - calendarIcon. Widt
}
}
protected override void OnHandleCreated ( EventArgs e )
{
base . OnHandleCreated ( e ) ;
int iconWidth = GetIconButtonWidth () ;
iconButtonArea = new RectangleF ( this . Width - iconWidth, 0 , iconWidth
}
protected override void OnMouseMove ( MouseEventArgs e )
{
base . OnMouseMove ( e ) ;
if ( iconButtonArea. Contains ( e. Location ))
this . Cursor = Cursors. Hand ;
else this . Cursor = Cursors. Default ;
}
If you don't like overriding event methods, you can subscribe to events from the constructor.
I did it that way to speed up the video tutorial.
// Private methods
private int GetIconButtonWidth ()
{
int textWidh = TextRenderer. MeasureText ( this . Text , this . Font ) .
if ( textWidh < = this . Width - ( calendarIconWidth + 20 ))
return calendarIconWidth;
else return arrowIconWidth;
}
That's it, however, some things still need to be optimized, for example, it does not have
support to change the time, with a little more work you can add this functionality or simply
create another time selector control, and if you see any problems, you can adjust the values
or add it.
https://rjcodeadvance.com/custom-datetimepicker-custom-controls-winform-c/ 7/12
7/22/2021 Custom DateTimePicker - Custom controls WinForm C # - RJ Code Advance
Video-tutorial
Leave a reply
Your email address will not be published. Required fields are marked with *
Commentary
Name *
https://rjcodeadvance.com/custom-datetimepicker-custom-controls-winform-c/ 8/12
7/22/2021 Custom DateTimePicker - Custom controls WinForm C # - RJ Code Advance
E-mail *
Web
Welcome to blog
Look for …
Follow me
Category:
.NET
ASP .NET
C#
Mistakes
F#
Visual basic
Windows Forms
https://rjcodeadvance.com/custom-datetimepicker-custom-controls-winform-c/ 9/12
7/22/2021 Custom DateTimePicker - Custom controls WinForm C # - RJ Code Advance
Layered Architecture
Database
MySQL
SQL Server
Custom controls
courses
.NET (Course)
Create .Net Installer
Full Login C #, VB, SQL Server
Software Patterns (Course)
OOP (Course)
Desk
GUI
Software Patterns
OOP
https://rjcodeadvance.com/custom-datetimepicker-custom-controls-winform-c/ 10/12
7/22/2021 Custom DateTimePicker - Custom controls WinForm C # - RJ Code Advance
Uncategorized
Web
Recent logins
Circular Picture Box - Border Gradient color + Styles - C # & WinForms
Custom ProgressBar - WinForms & C #
Custom TextBox Full - Rounded & Placeholder - WinForm, C #
Custom ComboBox - Icon, Back, Text & Border Color - WinForm C #
Custom Text Box - Custom controls WinForm C #
Recent comments
gustavo on Custom Button - Custom controls WinForm C #
_Nooker in Modern Form + Font Awesome Icons, WinForm, C # - VB.NET
https://rjcodeadvance.com/custom-datetimepicker-custom-controls-winform-c/ 11/12
7/22/2021 Custom DateTimePicker - Custom controls WinForm C # - RJ Code Advance
https://rjcodeadvance.com/custom-datetimepicker-custom-controls-winform-c/ 12/12