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 Properties
The following given below are some commonly used Properties of the DateTimePicker Control.
# | DateTimePicker Control in VB.net Properties | 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. |
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 Methods | Description |
---|---|---|
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. |
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 Events | 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. |
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:
Now, we select the Hired Date and End Date from the DateTimePicker Control.
After selecting the Hired Date and the End Date from the DateTimePicker Control, click on the Calculate button, and it shows the following result.
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.
PREVIOUS
NEXT