VB.Net - DateTimePicker Control

The DateTimePicker control allows selecting a date and time by editing the displayed values in the control. If you click the arrow in the DateTimePicker control, it displays a month calendar, like a combo box control. The user can make selection by clicking the required date. The new selected value appears in the text box part of the control.
The MinDate and the MaxDate properties allow you to put limits on the date range.
The following are some of the commonly used properties of the DateTimePicker control −
BackColor
Gets or sets a value indicating the background color of the DateTimePicker control.
BackgroundImage
Gets or sets the background image for the control.
BackgroundImageLayout
Gets or sets the layout of the background image of the DateTimePicker control.
CalendarFont
Gets or sets the font style applied to the calendar.
CalendarForeColor
Gets or sets the foreground color of the calendar.
CalendarMonthBackground
Gets or sets the background color of the calendar month.
CalendarTitleBackColor
Gets or sets the background color of the calendar title.
CalendarTitleForeColor
Gets or sets the foreground color of the calendar title.
CalendarTrailingForeColor
Gets or sets the foreground color of the calendar trailing dates.
Checked
Gets or sets a value indicating whether the Value property has been set with a valid date/time value and the displayed value is able to be updated.
CustomFormat
Gets or sets the custom date/time format string.
DropDownAlign
Gets or sets the alignment of the drop-down calendar on the DateTimePicker control.
ForeColor
Gets or sets the foreground color of the DateTimePicker control.
Format
Gets or sets the format of the date and time displayed in the control.
MaxDate
Gets or sets the maximum date and time that can be selected in the control.
MaximumDateTime
Gets the maximum date value allowed for the DateTimePicker control.
MinDate
Gets or sets the minimum date and time that can be selected in the control.
MinimumDateTime
Gets the minimum date value allowed for the DateTimePicker control.
PreferredHeight
Gets the preferred height of the DateTimePicker control.
RightToLeftLayout
Gets or sets whether the contents of the DateTimePicker are laid out from right to left.
ShowCheckBox
Gets or sets a value indicating whether a check box is displayed to the left of the selected date.
ShowUpDown
Gets or sets a value indicating whether a spin button control (also known as an up-down control) is used to adjust the date/time value.
Text
Gets or sets the text associated with this control.
Value
Gets or sets the date/time value assigned to the control.
The following are some of the commonly used methods of the DateTimePicker control −
ToString
Returns the string representing the control.
The following are some of the commonly used events of the DateTimePicker control −
BackColorChanged
Occurs when the value of the BackColor property changes.
BackgroundImageChanged
Occurs when the value of the BackgroundImage property changes.
BackgroundImageLayoutChanged
Occurs when the value of the BackgroundImageLayout property changes.
Click
Occurs when the control is clicked.
CloseUp
Occurs when the drop-down calendar is dismissed and disappears.
DoubleClick
Occurs when the control is double-clicked.
DragDrop
Occurs when a drag-and-drop operation is completed.
ForeColorChanged
Occurs when the value of the ForeColor property changes.
FormatChanged
Occurs when the Format property value has changed.
MouseClick
Occurs when the control is clicked with the mouse.
MouseDoubleClick
Occurs when the control is double-clicked with the mouse.
PaddingChanged
Occurs when the value of the Padding property changes.
Paint
Occurs when the control is redrawn.
RightToLeftLayoutChanged
Occurs when the RightToLeftLayout property changes.
TextChanged
Occurs when the value of the Text property changes.
ValueChanged
Occurs when the Value property changes.
In this example, let us create a small application for calculating days of leave. Let us add two DateTimePicker controls on the form, where the user will enter the date of going on leave and the date of joining. Let us keep a button control for performing the calculation and appropriate label controls for displaying information.
The form in design view −
Add the following code in the code editor window −
Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     ' Set the caption bar text of the form.  
      Me.Text = "tutorialspoint.com"
   End Sub
   
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Dim d1 As DateTime = DateTimePicker1.Value
      Dim d2 As DateTime = DateTimePicker2.Value
      Dim result As TimeSpan = d1.Subtract(d2)
      Dim days As Integer = result.TotalDays
      Label3.Text = days
   End Sub
End Class
When the above code is executed and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window −
Select two dates and click on the button for leave calculation −

    63 Lectures 
     4 hours 

 Frahaan Hussain

    103 Lectures 
     12 hours 

 Arnold Higuit

    60 Lectures 
     9.5 hours 

 Arnold Higuit

    97 Lectures 
     9 hours 

 Arnold Higuit
 Print
 Add Notes
 Bookmark this page