42. Date and time picker

When the example solution starts, it uses the following code to prepare the DateTimePicker control to allow the user to select both a date and time:

using System.Globalization;
using System.Threading;
...
// Make the DateTimePicker use a custom format
// that includes both date and time in localized long formats.
private void Form1_Load(object sender, EventArgs e)
{
// Uncomment to test in German.
//Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");

// Get the computer's culture info.
CultureInfo info = Thread.CurrentThread.CurrentCulture;

// Set the format to use long date and short time.
dateAndTimePicker.Format = DateTimePickerFormat.Custom;
dateAndTimePicker.CustomFormat =
info.DateTimeFormat.LongDatePattern + " " +
info.DateTimeFormat.ShortTimePattern;

// Display the initial date and time.
dateAndTimeLabel.Text = dateAndTimePicker.Value.ToString("F");
}

The code begins with using directives for the System.Globalization and System.Threading namespaces because it needs those namespaces to work with localization information.

The form's Load event handler begins with a commented statement that sets the thread's culture to German. Uncomment that code to see how the program would behave if it were set up to use the German locale.

Note that this only changes the way the code displays the selected date and time. It does not make the DateTimePicker use the German names for months and days of the week because the control has already been created before this code executes.

Next, the code gets the thread's CultureInfo object. That object holds localization information, including such things as the formats that the locale uses for dates, times, numbers, and currency.

The code sets the DateTimePicker control's Format property to Custom. It then sets the control's CustomFormat property to the locale's long date pattern, followed by some spaces and the locale's short time pattern. Because it takes the date and time patterns from the CultureInfo object, the DateTimePicker displays the date and time appropriately for the current locale.

The event handler finishes by displaying the initial date and time in a label control. The "F" format flag indicates that ToString should use the full format with long date and time values. Standard format specifiers such as this one are locale-aware, so the result is displayed appropriately for the computer's locale. (In contrast, custom specifiers such as "mm/dd/yyyy" are not locale-aware, so they may be confusing to users in some locales.)

When the user changes the value selected in the DateTimePicker control, the following event handler updates the label to show the new selection:

// Display the selected date and time in long formats.
private void dateAndTimePicker_ValueChanged(object sender, EventArgs e)
{
dateAndTimeLabel.Text = dateAndTimePicker.Value.ToString("F");
}

This code simply displays the selected time in the form's label control.

The following screenshot shows the example program in action:

Notice that the DateTimePicker control allows the user to select the date, hours, and minutes, but not seconds. However, the label displays 11 seconds after the minute. If you don't let the user set all of the control's value fields, be aware that some of them, such as the seconds in this example, may not be zero.
If you click on the drop-down icon on the DateTimePicker control's right, a MonthCalendar control appears. If you don't want the user to see that drop-down, set the DateTimePicker control's ShowUpDown property to true to make the control replace the drop-down icon with tiny up and down buttons.

Download the DateAndTimePicker example solution to see additional details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset