VB.Net - DateTimePicker Control

Advertisements


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.

VB.Net DateTimePicker Control

The MinDate and the MaxDate properties allow you to put limits on the date range.

Properties of the DateTimePicker Control

The following are some of the commonly used properties of the DateTimePicker control:

S.NPropertyDescription
1BackColorGets or sets a value indicating the background color of the DateTimePicker control.
2BackgroundImageGets or sets the background image for the control.
3BackgroundImageLayoutGets or sets the layout of the background image of the DateTimePicker control.
4CalendarFontGets or sets the font style applied to the calendar.
5CalendarForeColorGets or sets the foreground color of the calendar.
6CalendarMonthBackgroundGets or sets the background color of the calendar month.
7CalendarTitleBackColorGets or sets the background color of the calendar title.
8CalendarTitleForeColorGets or sets the foreground color of the calendar title.
9CalendarTrailingForeColorGets or sets the foreground color of the calendar trailing dates.
10CheckedGets 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.
11CustomFormatGets or sets the custom date/time format string.
12DropDownAlignGets or sets the alignment of the drop-down calendar on the DateTimePicker control.
13ForeColorGets or sets the foreground color of the DateTimePicker control.
14FormatGets or sets the format of the date and time displayed in the control.
15MaxDateGets or sets the maximum date and time that can be selected in the control.
16MaximumDateTimeGets the maximum date value allowed for the DateTimePicker control.
17MinDateGets or sets the minimum date and time that can be selected in the control.
18MinimumDateTimeGets the minimum date value allowed for the DateTimePicker control.
19PreferredHeightGets the preferred height of the DateTimePicker control.
20RightToLeftLayoutGets or sets whether the contents of the DateTimePicker are laid out from right to left.
21ShowCheckBoxGets or sets a value indicating whether a check box is displayed to the left of the selected date.
22ShowUpDownGets 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.
23TextGets or sets the text associated with this control.
24ValueGets or sets the date/time value assigned to the control.

Methods of the DateTimePicker Control

The following are some of the commonly used methods of the DateTimePicker control:

S.NMethod Name & Description
1ToString
Returns the string representing the control.

Events of the DateTimePicker Control

The following are some of the commonly used events of the DateTimePicker control:

S.NEventDescription
1BackColorChangedOccurs when the value of the BackColor property changes.
2BackgroundImageChangedOccurs when the value of the BackgroundImage property changes.
3BackgroundImageLayoutChangedOccurs when the value of the BackgroundImageLayout property changes.
4ClickOccurs when the control is clicked.
5CloseUpOccurs when the drop-down calendar is dismissed and disappears.
6DoubleClickOccurs when the control is double-clicked.
7DragDropOccurs when a drag-and-drop operation is completed.
8ForeColorChangedOccurs when the value of the ForeColor property changes.
9FormatChangedOccurs when the Format property value has changed.
10MouseClickOccurs when the control is clicked with the mouse.
11MouseDoubleClickOccurs when the control is double-clicked with the mouse.
12PaddingChangedOccurs when the value of the Padding property changes.
13PaintOccurs when the control is redrawn.
14RightToLeftLayoutChangedOccurs when the RightToLeftLayout property changes.
15TextChangedOccurs when the value of the Text property changes.
16ValueChangedOccurs when the Value property changes.

Example

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:

DateTimePicker Example Form

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:

DateTimePicker Result Form

Select two dates and click on the button for leave calculation:

DateTimePicker Result Form

Advertisements
Advertisements