DateTimePicker
This presents an interface
for date selection. It allows the user to select a valid DateTime
without much effort on your part.
Please add the DateTimePicker
control to your Form, and then add the ValueChanged
event handler by right-clicking on the DateTimePicker
, and going to Properties.
Here we get the DateTime
value for yesterday. This is done with Today, Subtract, and TimeSpan
. The Value property is assigned to the result.
DateTime
, this event handler is executed. The window's title shows the DateTime
that was selected.using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Initialize picker to yesterday. DateTime result = DateTime.Today.Subtract(TimeSpan.FromDays(1)); dateTimePicker1.Value = result; } private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { // Set title bar to selected date. DateTime result = dateTimePicker1.Value; this.Text = result.ToString(); } } }
ShowCheckBox
The ShowCheckBox
property changes the DateTimePicker
to display a check box on the left side. The user can enable or disable the DateTimePicker
this way.
DateTimePicker
.ShowUpDown
The ShowUpDown
property changes the DateTimePicker
such that it displays an up and down arrow box on the right side instead of the drop-down menu.
ShowUpDown
to True when the DateTime
will only need to be changed up or down by a couple units.The DateTimePicker
control can be used with the Value property and ValueChanged
event handler. We can change the UI with ShowCheckBox
and ShowUpDown
.