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 −

Sr.No. Property & Description
1

BackColor

Gets or sets a value indicating the background color of the DateTimePicker control.

2

BackgroundImage

Gets or sets the background image for the control.

3

BackgroundImageLayout

Gets or sets the layout of the background image of the DateTimePicker control.

4

CalendarFont

Gets or sets the font style applied to the calendar.

5

CalendarForeColor

Gets or sets the foreground color of the calendar.

6

CalendarMonthBackground

Gets or sets the background color of the calendar month.

7

CalendarTitleBackColor

Gets or sets the background color of the calendar title.

8

CalendarTitleForeColor

Gets or sets the foreground color of the calendar title.

9

CalendarTrailingForeColor

Gets or sets the foreground color of the calendar trailing dates.

10

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.

11

CustomFormat

Gets or sets the custom date/time format string.

12

DropDownAlign

Gets or sets the alignment of the drop-down calendar on the DateTimePicker control.

13

ForeColor

Gets or sets the foreground color of the DateTimePicker control.

14

Format

Gets or sets the format of the date and time displayed in the control.

15

MaxDate

Gets or sets the maximum date and time that can be selected in the control.

16

MaximumDateTime

Gets the maximum date value allowed for the DateTimePicker control.

17

MinDate

Gets or sets the minimum date and time that can be selected in the control.

18

MinimumDateTime

Gets the minimum date value allowed for the DateTimePicker control.

19

PreferredHeight

Gets the preferred height of the DateTimePicker control.

20

RightToLeftLayout

Gets or sets whether the contents of the DateTimePicker are laid out from right to left.

21

ShowCheckBox

Gets or sets a value indicating whether a check box is displayed to the left of the selected date.

22

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.

23

Text

Gets or sets the text associated with this control.

24

Value

Gets 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 −

Sr.No. Method Name & Description
1

ToString

Returns the string representing the control.

Events of the DateTimePicker Control

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

Sr.No. Event & Description
1

BackColorChanged

Occurs when the value of the BackColor property changes.

2

BackgroundImageChanged

Occurs when the value of the BackgroundImage property changes.

3

BackgroundImageLayoutChanged

Occurs when the value of the BackgroundImageLayout property changes.

4

Click

Occurs when the control is clicked.

5

CloseUp

Occurs when the drop-down calendar is dismissed and disappears.

6

DoubleClick

Occurs when the control is double-clicked.

7

DragDrop

Occurs when a drag-and-drop operation is completed.

8

ForeColorChanged

Occurs when the value of the ForeColor property changes.

9

FormatChanged

Occurs when the Format property value has changed.

10

MouseClick

Occurs when the control is clicked with the mouse.

11

MouseDoubleClick

Occurs when the control is double-clicked with the mouse.

12

PaddingChanged

Occurs when the value of the Padding property changes.

13

Paint

Occurs when the control is redrawn.

14

RightToLeftLayoutChanged

Occurs when the RightToLeftLayout property changes.

15

TextChanged

Occurs when the value of the Text property changes.

16

ValueChanged

Occurs 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
vb.net_basic_controls.htm
Advertisements