DateTimePicker: Properties, Events, and Methods Examples
Here are some examples of properties, events, and methods of the DateTimePicker control in Windows
Forms:
Properties:
1. Value: Gets or sets the date/time value assigned to the DateTimePicker.
DateTime selectedDate = dateTimePicker1.Value; // Get the selected date/time value
dateTimePicker1.Value = DateTime.Now; // Set the current date/time as the selected value
2. Format: Gets or sets the format of the date/time displayed in the DateTimePicker.
dateTimePicker1.Format = DateTimePickerFormat.Short; // Display the short date format
3. ShowUpDown: Gets or sets a value indicating whether the up-down control is displayed.
dateTimePicker1.ShowUpDown = true; // Display the up-down control for time selection
Events:
1. ValueChanged: Occurs when the value of the DateTimePicker control changes.
dateTimePicker1.ValueChanged += DateTimePicker1_ValueChanged;
private void DateTimePicker1_ValueChanged(object sender, EventArgs e)
// Handle the value changed event
2. CloseUp: Occurs when the drop-down calendar is closed.
dateTimePicker1.CloseUp += DateTimePicker1_CloseUp;
private void DateTimePicker1_CloseUp(object sender, EventArgs e)
// Handle the close up event
Methods:
1. SetRange: Sets the minimum and maximum allowable dates that can be selected.
dateTimePicker1.SetRange(minDate, maxDate); // Set the range of selectable dates
2. DroppedDown: Gets or sets a value indicating whether the drop-down calendar is currently
displayed.
bool isDroppedDown = dateTimePicker1.DroppedDown; // Check if the drop-down calendar is
displayed
dateTimePicker1.DroppedDown = true; // Display the drop-down calendar
3. ResetValue: Resets the value of the DateTimePicker to its default value.
dateTimePicker1.ResetValue(); // Reset the value of the DateTimePicker
These are just a few examples of the properties, events, and methods available for
the DateTimePicker control in Windows Forms. You can explore the full range of options in the
documentation or by using IntelliSense in your development environment to discover more capabilities
and tailor them to your specific requirements.
Here are some additional details about the DateTimePicker control in Windows Forms:
Properties:
1. CustomFormat: Gets or sets the custom date/time format string.
dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm:ss"; // Set a custom date/time format
2. MinDate: Gets or sets the minimum date that can be selected in the DateTimePicker.
dateTimePicker1.MinDate = new DateTime(2000, 1, 1); // Set the minimum selectable date
3. MaxDate: Gets or sets the maximum date that can be selected in the DateTimePicker.
dateTimePicker1.MaxDate = new DateTime(2030, 12, 31); // Set the maximum selectable date
Events:
1. DropDown: Occurs when the drop-down calendar is shown.
dateTimePicker1.DropDown += DateTimePicker1_DropDown;
private void DateTimePicker1_DropDown(object sender, EventArgs e)
// Handle the drop-down event
2. KeyDown: Occurs when a key is pressed while the DateTimePicker has focus.
dateTimePicker1.KeyDown += DateTimePicker1_KeyDown;
private void DateTimePicker1_KeyDown(object sender, KeyEventArgs e)
{
// Handle the key down event
Methods:
1. ShowCheckBox: Displays or hides the checkbox that allows a null value in the DateTimePicker.
dateTimePicker1.ShowCheckBox = true; // Show the checkbox for a null value
2. UpdateLayout: Forces the control to update its layout.
dateTimePicker1.UpdateLayout(); // Update the layout of the DateTimePicker control
3. Focus: Sets input focus to the DateTimePicker.
dateTimePicker1.Focus(); // Set focus to the DateTimePicker control
These additional properties, events, and methods provide further functionality and control over
the DateTimePicker control in Windows Forms. You can explore their usage and adapt them to meet
your specific application requirements.
Here are some more details about the DateTimePicker control in Windows Forms:
Properties:
1. CalendarForeColor: Gets or sets the foreground color of the calendar month displayed in the
drop-down.
dateTimePicker1.CalendarForeColor = Color.Red; // Set the foreground color of the calendar
2. CalendarTitleForeColor: Gets or sets the foreground color of the calendar title.
dateTimePicker1.CalendarTitleForeColor = Color.Blue; // Set the foreground color of the
calendar title
3. Checked: Gets or sets a value indicating whether the DateTimePicker is checked.
bool isChecked = dateTimePicker1.Checked; // Check if the DateTimePicker is checked
dateTimePicker1.Checked = true; // Set the DateTimePicker as checked
Events:
1. FormatChanged: Occurs when the Format property of the DateTimePicker changes.
dateTimePicker1.FormatChanged += DateTimePicker1_FormatChanged;
private void DateTimePicker1_FormatChanged(object sender, EventArgs e)
// Handle the format changed event
}
2. TextChanged: Occurs when the Text property of the DateTimePicker changes.
dateTimePicker1.TextChanged += DateTimePicker1_TextChanged;
private void DateTimePicker1_TextChanged(object sender, EventArgs e)
// Handle the text changed event
Methods:
1. SetToNull: Sets the DateTimePicker value to null.
dateTimePicker1.SetToNull(); // Set the DateTimePicker value to null
2. CloseUp: Closes the drop-down calendar.
dateTimePicker1.CloseUp(); // Close the drop-down calendar
3. Update: Forces the control to invalidate its client area and immediately redraw itself and any
child controls.
dateTimePicker1.Update(); // Update the DateTimePicker control and its child controls
These additional properties, events, and methods provide further flexibility and control over
the DateTimePicker control in Windows Forms. You can explore their usage and adapt them to suit your
specific application requirements.
Here are some more details about the DateTimePicker control in Windows Forms:
Properties:
1. RightToLeftLayout: Gets or sets whether the layout of the control is right-to-left.
dateTimePicker1.RightToLeftLayout = true; // Set the control layout to right-to-left
2. ShowUpDown: Gets or sets whether the control displays up and down buttons for changing the
date and time values.
dateTimePicker1.ShowUpDown = true; // Display up and down buttons for changing values
3. CustomFormat: Gets or sets the custom date/time format string used when the Format property
is set to Custom.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm:ss"; // Set a custom format for
date/time
Events:
1. Enter: Occurs when the control receives focus.
dateTimePicker1.Enter += DateTimePicker1_Enter;
private void DateTimePicker1_Enter(object sender, EventArgs e)
// Handle the enter event
2. Leave: Occurs when the control loses focus.
dateTimePicker1.Leave += DateTimePicker1_Leave;
private void DateTimePicker1_Leave(object sender, EventArgs e)
// Handle the leave event
Methods:
1. Undo: Reverses the last operation performed on the control's value.
dateTimePicker1.Undo(); // Undo the last operation on the control's value
2. BeginUpdate: Prevents the control from updating until the EndUpdate method is called.
dateTimePicker1.BeginUpdate(); // Prevent the control from updating
// Perform multiple operations on the control
dateTimePicker1.EndUpdate(); // Allow the control to update
3. GetLifetimeService: Retrieves the current lifetime service object that controls the lifetime policy
for this instance.
var lifetimeService = dateTimePicker1.GetLifetimeService(); // Get the lifetime service object
These additional properties, events, and methods provide further customization and control options for
the DateTimePicker control in Windows Forms. You can explore their usage and adapt them to suit your
specific application requirements.