Intermediate
8 min read
Control #6
DateTimePicker Control
The DateTimePicker allows users to select a date and/or time from a calendar dropdown.
What is a DateTimePicker?
DateTimePicker displays a date that users can change by clicking the calendar icon. It can show dates, times, or both. Perfect for birthday selection, appointment booking, and scheduling.
Code Example
// Set properties
dtpBirthday.Format = DateTimePickerFormat.Short;
dtpBirthday.Value = new DateTime(2000, 1, 1);
dtpBirthday.MaxDate = DateTime.Today;
// Get selected date
private void btnSubmit_Click(object sender, EventArgs e)
{
DateTime selected = dtpBirthday.Value;
MessageBox.Show($"Selected: {selected.ToShortDateString()}");
}
Exercise
Task: Create a birthday reminder app.
- Add a DateTimePicker for birthdate selection.
- Add a Button that shows the user's age.
- Add a CheckBox to show/hide the time selection.
- Display the selected date in a Label.
Key Takeaway
DateTimePicker makes date selection easy and consistent. Use Value
to get the selected date as a DateTime.