How do you set a date and time in a date-time picker? You simply edit the date and/or time displayed in the control, as you see in Figure 8.14. To make that easier, if the user clicks the down arrow in a date-time picker, a drop-down month calendar appears by default, and you can make date selections from that calendar.
When the date and/or time changes, a ValueChanged event occurs, which I handle in the Pickers example on the CD-ROM. You can get the new, selected date and time with the Text property of the control, or the Value property, which returns a DateTime object (see Tables 8.1–8.3 for more on DateTime objects). In the Pickers example, I copy the selected date to a text box, like this:
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged TextBox1.Text = "Date selected: " & DateTimePicker1.Text End Sub
You can see the results of this code in Figure 8.15, where I've selected a new date.
This is all very well, but how do you set the format for the date and time displayed in the control? By default, date-time pickers don't even let the user edit the time, for example. You can set the format with these members of the DateTimePicker Format enumeration:
Custom— The date-time control displays the date-time value in a custom format.
Long— The date-time control displays the date-time value in the long date format set by the user's operating system.
Short— The date-time control displays the date-time value in the short date format set by the user's operating system.
Time— The date-time control displays the date-time value in the time format set by the user's operating system.
You also can customize the display of this control. For more on this subject, take a look at the next topic.