DateTimePicker Control in VB.net – Properties, Method and Events

What is DateTimePicker Control in VB.net?

In Windows Forms, the DateTimePicker Control in VB.net lets the user choose or show date and time values in a certain format.

Using the VB.net DateTimePicker Control’s Value property, we can also find out what the current date and time are.

By default, the Value property of the DateTimePicker gives back the current date and time.

Let’s create a DateTimePicker in VB.net by dragging a DateTimePicker Control from the Toolbox and dropping it on the form.

DateTimePicker Control in VB.net Tutorial

DateTimePicker Control in VB.net Properties

The following given below are some commonly used Properties of the DateTimePicker Control.

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

DateTimePicker Control in VB.net Methods

The following given below are some commonly used Methods of the DateTimePicker Control in VB.net.

#DateTimePicker Control in VB.net MethodsDescription
1.Contains(Control)It is used to validate whether the specified control is a child of the DateTimePicker control or not.
2.CreateControl()It is used to force the creation of visible control to handle the creation and any visible child controls.
3.GetAutoSizeMode()The GetAutoSizeMode() method is used to check the behavior of the DateTimePicker control when the AutoAize property is enabled.
4.ResetBackColor()It is used to reset the back color of the DateTimePicker control.
5.Select()The Select() method is used to start or activate the DateTimePicker control.
6.Show()The Show() method is used to display the control to the user.
7.ToString()The ToString() method is used to return a string that represents the current DateTimePicker control.
Methods and Descriptions of DateTimePicker Control in VB.net

DateTimePicker Control in VB.net Events

The following given below are some commonly used Events of the DateTimePicker Control in VB.net.

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

Let’s create a program of DateTimePicker Control in the VB.NET Form.

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DateTimePicker1.Format = DateTimePickerFormat.Long
    End Sub

    Private Sub Button1_Click_3(sender As Object, e As EventArgs) Handles Button1.Click
        Dim dp As Date = DateTimePicker1.Value
        Dim dp2 As Date = DateTimePicker2.Value
        Dim result As TimeSpan = dp2.Subtract(dp)
        Dim ds As Integer = result.TotalDays
        TextBox1.Text = ds
        TextBox1.ForeColor = ForeColor.Red
        MsgBox(" Days = " & ds)
    End Sub
End Class

Program Output:

DateTimePicker Control in VB.net Output
DateTimePicker Control in VB.net Output

Now, we select the Hired Date and End Date from the DateTimePicker Control.

DateTimePicker Control in VB.net Select Hired Date and End Date
DateTimePicker Control in VB.net Select Hired Date and End Date

After selecting the Hired Date and the End Date from the DateTimePicker Control, click on the Calculate button, and it shows the following result.

DateTimePicker Control in VB.net Display Results
DateTimePicker Control in VB.net Display Results

Summary

In this article, we talked about how to make a Windows Forms DateTimePicker Control in VB.net using Microsoft Visual Studio both at design-time and at run-time.

Then we learned how to use the different Properties, Methods, and Events.


Leave a Comment