Visual Basic .NET - DateTimePicker Control
The DateTimePicker control in VB.NET allows users to select a date and time from a graphical calendar and clock interface. Here are some of its basic properties, methods, and events:
Properties
- Value: Gets or sets the selected date and time.
- Format: Gets or sets the format of the displayed date and time.
- CustomFormat: Gets or sets a custom date and time format string.
- MinDate: Gets or sets the minimum date and time that can be selected.
- MaxDate: Gets or sets the maximum date and time that can be selected.
- ShowUpDown: Gets or sets a value indicating whether a spin button control is displayed.
Methods
- ResetText(): Resets the text displayed in the control to its default value.
- UpdateCustomFormat(): Updates the custom date and time format string.
Events
- ValueChanged: Occurs when the Value property changes.
Here's an example of how to use the DateTimePicker control:
Public Class Form1
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
' Display the selected date and time in a message box
MessageBox.Show("Selected Date and Time: " & DateTimePicker1.Value.ToString())
End Sub
End Class
In this example, a DateTimePicker control is added to a form. The ValueChanged event is handled to display the selected date and time in a message box when the user changes the value of the control.
Public Class Form1
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
' Update the label text with the selected date and time
Label1.Text = "Selected Date and Time: " & DateTimePicker1.Value.ToString()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Set the minimum date to today's date
DateTimePicker1.MinDate = DateTime.Today
' Set the maximum date to 1 year from today's date
DateTimePicker1.MaxDate = DateTime.Today.AddYears(1)
' Set the custom date format to display only the date
DateTimePicker1.Format = DateTimePickerFormat.Short
End Sub
End Class
This example demonstrates how to update a label with the selected date and time whenever the value of the DateTimePicker control changes. It also shows how to set the minimum and maximum dates, as well as the custom date format using a button click event.