![]() |
The Date/Time Picker Control |
|
Description |
The Date and Time Picker is a control that allows the user to select either a date or a time value. This control provides two objects in one:
![]() |
|
![]() |
![]() |
One of the advantages of the Date and Time Picker control is that it allows the user to select a time or a date value instead of typing it. This tremendously reduces the likelihood of mistakes.
|
Creating a Date/Time Picker |
To create a Date or Time Picker control, add a DateTimePicker control
to a form or other container.
The DateTimePicker control is based on the DateTimePicker class. In
reality, the DateTimePicker control can be considered as two controls in one:
you just have to choose which one of both controls you want to use.
|
Introduction to the Date Picker |
After adding the DateTimePicker control to the form, to allow the user to set the dates and not the times on the control, set its Format property either to Long or to Short.
|
|
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Characteristics of the Date Picker |
|
The Spin Button |
After adding a date time picker control and setting its Format to either Long (the default) or Short, the control becomes a combo box (the default). If you do not like the combo box, you can display a spin button instead. This characteristic is controlled by the ShowUpDown Boolean property. When its value is set to False (the default), the control appears as a combo box. It you set it to True, it appears as a spin button:

If the control displays a combo box and if the user clicks the arrow on the Date control, a calendar object similar to the
month calendar control displays:

|
The Check Box |
In some cases, you may want to decide when to allow the user to select a date or when to disable it. There are two ways you can do. You can use the Control's Enabled property that all other controls inherit. Another technique you can use is to display a check box on the left side of the text box section of the control. The presence or absence of the check box is controlled by the ShowCheckBox Boolean property whose default value is False. If you set it to True, a check box appears:

When the check box is checked, the user can change the displayed date. When the check box is unchecked, the control is displayed and the user cannot change the date. The user must first put a check mark in the check box in order to be able to change the date.
The state of the check box is controlled by the Boolean Checked property. If the user clicks the check box to put a check mark in it, this property's value becomes true.
|
Using the Date Picker |
As its name indicates, the date picker either displays a date or it allows the user to specify a date. The control follows the format of the date values specified in the regional settings of Control Panel. Consequently, the date is made of various sections including the name of the day, the name of the month, the numeric day in the month, and the year.
If the control is equipped with a spin button, to change the month, the user can click the month and then click one of the arrow buttons of the spin control. The user can also use the arrow keys to get the same effect. In the same way, the user can change the values of the day or the year.
If the control appears as a combo box, the user can click the arrow button. This would display a calendar:

When the calendar displays, the control throws a DropDown event. The DropDown event is of type EventArgs. This means that this event does not carry any significant information, other than to let you know that the calendar of the control has been dropped to display.
While the calendar is displaying, the user can change the month, change the year, or click a date to select one. T user can still change the date in the text box side of the control. However it is done, on the text box or on the calendar, when the date of the control has been changed, the control fires a ValueChanged event. The ValueChanged event, which is the default event of the control, is of type EventArgs, meaning it does not give you any detail about the date that was selected or about anything the user did. You would use your own means of finding out what date the user had selected or specified. This can easily be done by getting the Value property of the control.
If the control is displaying a calendar, once the user clicks a date, the calendar disappears and the control becomes a combo box again. When the calendar retracts, the control fires a CloseUp event. The CloseUp event is of type EventArgs, which means it does not carry any particular information other than letting you know that the calendar has been closed.
|
|
private void dtpEndDate_CloseUp(object sender, EventArgs e)
{
double Principal = 0.00D,
InterestRate = 0.00D,
Periods = 0.00D;
double InterestEarned, FutureValue;
DateTime StartDate, EndDate;
TimeSpan spnPeriods;
// Get the value of the principal
try
{
Principal = double.Parse(txtPrincipal.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Principal Value");
}
// Get the interest rate
try
{
InterestRate = double.Parse(txtInterestRate.Text) / 100;
}
catch (FormatException)
{
MessageBox.Show("Invalid Interest Rate");
}
// Get the start and end dates
StartDate = dtpStartDate.Value;
EndDate = dtpEndDate.Value;
// Make sure the end date doesn't occur before the start date
if (EndDate < StartDate)
{
MessageBox.Show("Invalid Date Sequence: " +
"the end date must occur after the start date");
dtpEndDate.Value = DateTime.Today;
return;
}
// Get the difference in days
// The will be the periods
spnPeriods = EndDate.Subtract(StartDate);
int days = spnPeriods.Days;
txtPeriods.Text = days.ToString();
double p = 0.00D;
// Because we will allow the user to directly specify
// the number of days, let's get the period from its text box
try
{
p = double.Parse(txtPeriods.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid number of days");
}
// The actual period is gotten as follows
Periods = p / 365;
// Now we can perform the calculations
InterestEarned = Principal * InterestRate * Periods;
FutureValue = Principal + InterestEarned;
// Display the values
txtInterestEarned.Text = InterestEarned.ToString("C");
txtFutureValue.Text = FutureValue.ToString("C");
}private void txtPeriods_Leave(object sender, EventArgs e)
{
// If the user directly enters the number of days
// in the Periods text box and press Tab,
// we can perform the caltulation
dtpEndDate_CloseUp(sender, e);
}private void btnCalculate_Click(object sender, EventArgs e)
{
// If the user directly enters the number of days
// in the Periods text box and clicks the Calculate
// button, we can perform the caltulation
dtpEndDate_CloseUp(sender, e);
}private void btnClose_Click(object sender, EventArgs e)
{
Close();
}|
The Calendar Visual Characteristics |
If the date picker control displays like a combo box, as mentioned already, if the user clicks the arrow button, a calendar appears. The calendar shares the same functionality and characteristics like the month calendar control:
|
|
![]() |
![]() |
![]() |
|
The Alignment of the Calendar |
This calendar displays to the bottom-left or the bottom-right side of the combo box. To control this alignment, change the value of the
DropDownAlign property whose default value is Left. The other
value is Right.
The displayed calendar object allows the user to select a date using the same techniques we described for the
alendar control. The calendar
part of the date time picker control displays using the same colors and other properties as we saw with the
calendar control. After the user has selected a date, the date value displays in the
text box section of the combo box and the calendar disappears.
|
The Minimum and the Maximum Dates |
If you want to control the range of dates the user can select, use the MinDate and the MaxDate properties as we mentioned them from the MonthCalendar control.
|
The Value of the Calendar |
When you add the date time picker control to your form or container, it displays the date of the computer at the time the control was added. If you want the control to display a different date, set the desired value in the Value field of the Properties window. At any time, you can find out what value the Date Picker has by retrieving the value of the Value property.
|
The Custom Format of the Calendar |
If you set the Format property to Long, the date displays using the Long Date formats of Control Panel. If you set the Format to Short, the control uses the Short Date value of Control Panel. If you want to customize the way the date is displayed, set the Format property to Custom. After setting the Format to Custom, use the CustomFormat property to create the desired format. The format is created by combining the following characters:
| Format | Used For | Description |
| d | Days | Displays the day as a number from 1 to 31 |
| dd | Days | Displays the day as a number with a leading 0 if the number is less than 10 |
| ddd | Weekdays | Displays a weekday name with 3 letters as Mon, Tue, etc |
| dddd | Weekdays | Displays the complete name of a week day as Monday, etc |
| M | Months | Displays the numeric month from 1 to 12 |
| MM | Months | Displays the numeric month with a leading 0 if the number is less than 10 |
| MMM | Months | Displays the short name of the month as Jan, Feb, Mar, etc |
| MMMM | Months | Displays the complete name of the month as January, etc |
| yy | Years | Displays two digits for the year as 00 for 2000 or 03 for 2003 |
| yyyy | Years | Displays the numeric year with 4 digits |
This means that you should be reluctant to let the users type whatever they want. The less they type, the less checking you need to do.
The Date Time Picker control uses some events that the month calendar control does not have. Whenever the user changes the date or time value of the control, a ValueChanged event fires. You can use this event to take some action such as indicating to the user that the new date is invalid.
If the Format property of the control is set to Date and if the ShowUpDown property is set to False, when the user clicks the arrow of the combo box to display the calendar part of the date time picker control, the DropDown event fires. On the other hand, if the user clicks the arrow to retract the calendar, the CloseUp event fires. Both events are of EventArgs type.
|
Introduction to the Time Picker |
|
Introduction |
As described already, the date time picker is two controls in one object. To transform a date time picker into a time picker control, change its Format property to a Time value:
Although optional, you should (with emphasis) also set the ShowUpDown Boolean property to True. This makes it a true time picker control:

If you do not set the ShowUpDown property to true, the control would display as a combo box and when the user clicks the arrow button, a calendar would come, which does not make sense since the purpose of the control is to deal with time values.
|
|
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Using the Time Picker |
The time picker is meant either to only display a time to the user or to both display time and allow the user to specify a time. The control follows the format of the time values set in the regional settings of Control Panel. By default, in US English, the time is divided in four sections that include the hour value, the minute value, the second value, and the AM/PM side. These sections are separated by standard characters that also are specified in the regional settings of Control Panel.
To change the time, the user can click a section, such as the hour, the minute, the second, or the AP/PM side, then click one of the arrows of the spin button. The up button increases (only) the value of the selected section while the down button decreases (only) the value of the selected section. The user can also use the arrow keys to change the value of a section.
After changing the value of the time, even the change occurs for only one section, the control fires a ValueChanged event, which is the default event of the control.
|
Characteristics of the Time Picker |
|
The Value of the Time Picker |
As mentioned already, the user changes the time by using the various sections of the control. If the user changes the value of a section, it is considered that the whole time has been changed. At any time, the (current) time of the control is stored in the Value property. You can use this property either to programmatically change the time on the control or to get the time on the control.
|
|
private void dtpTimeLeft_ValueChanged(object sender, EventArgs e)
{
DateTime dateLeft = this.dtpDateLeft.Value;
DateTime timeLeft = this.dtpTimeLeft.Value;
DateTime time9AM = new DateTime(timeLeft.Year, timeLeft.Month, timeLeft.Day, 9, 0, 0);
// If the customer leaves clothes before 9AM...
if (timeLeft <= time9AM)
{
// ... then they should be ready the same day after 5PM
this.dtpDateExpected.Value = dateLeft;
this.dtpTimeExpected.Value = new DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day, 17, 0, 0);
}
else
{
// If the clothes were left after 9AM, they will be available the following morning at 8AM
this.dtpDateExpected.Value = new DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day + 1);
this.dtpTimeExpected.Value = new DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day + 1, 8, 0, 0);
}
}private void btnCalculate_Click(object sender, EventArgs e)
{
decimal unitPriceShirts = 0.00M, unitPricePants = 0.00M,
unitPriceItem1 = 0.00M, unitPriceItem2 = 0.00M,
unitPriceItem3 = 0.00M, unitPriceItem4 = 0.00M;
decimal subTotalShirts = 0.00M, subTotalPants = 0.00M,
subTotalItem1 = 0.00M, subTotalItem2 = 0.00M,
subTotalItem3 = 0.00M, subTotalItem4 = 0.00M;
int qtyShirts = 1, qtyPants = 1, qtyItem1 = 1,
qtyItem2 = 1, qtyItem3 = 1, qtyItem4 = 4;
decimal cleaningTotal = 0.00M, taxRate = 0.00M,
taxAmount = 0.00M, netPrice = 0.00M;
// Retrieve the unit price of this item
// Just in case the user types an invalid value, we are using a try...catch
try
{
unitPriceShirts = decimal.Parse(this.txtUnitPriceShirts.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered for the price of shirts is not valid" +
"\nPlease try again");
return;
}
// Retrieve the number of this item
// Just in case the user types an invalid value, we are using a try...catch
try
{
qtyShirts = int.Parse(this.txtQuantityShirts.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered for the number of shirts is not valid" +
"\nPlease try again");
return;
}
try
{
unitPricePants = decimal.Parse(this.txtUnitPricePants.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered for the price of pants is not valid" +
"\nPlease try again");
return;
}
try
{
qtyPants = int.Parse(this.txtQuantityPants.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered for the number of pants is not valid" +
"\nPlease try again");
return;
}
if ((cbxItem1.Text == "None") || (cbxItem1.Text == ""))
{
qtyItem1 = 0;
unitPriceItem1 = 0.00M;
}
else
{
try
{
unitPriceItem1 = decimal.Parse(this.txtUnitPriceItem1.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered for the price is not valid" +
"\nPlease try again");
return;
}
try
{
qtyItem1 = int.Parse(this.txtQuantityItem1.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered is not valid" +
"\nPlease try again");
return;
}
}
if ((cbxItem2.Text == "None") || (cbxItem2.Text == ""))
{
qtyItem2 = 0;
unitPriceItem2 = 0.00M;
}
else
{
try
{
unitPriceItem2 = decimal.Parse(this.txtUnitPriceItem2.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered for the price is not valid" +
"\nPlease try again");
return;
}
try
{
qtyItem2 = int.Parse(this.txtQuantityItem2.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered is not valid" +
"\nPlease try again");
return;
}
}
if ((cbxItem3.Text == "None") || (cbxItem3.Text == ""))
{
qtyItem3 = 0;
unitPriceItem3 = 0.00M;
}
else
{
try
{
unitPriceItem3 = decimal.Parse(this.txtUnitPriceItem3.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered for the price is not valid" +
"\nPlease try again");
return;
}
try
{
qtyItem3 = int.Parse(this.txtQuantityItem3.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered is not valid" +
"\nPlease try again");
return;
}
}
if ((cbxItem4.Text == "None") || (cbxItem4.Text == ""))
{
qtyItem4 = 0;
unitPriceItem4 = 0.00M;
}
else
{
try
{
unitPriceItem4 = decimal.Parse(this.txtUnitPriceItem4.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered for the price is not valid" +
"\nPlease try again");
return;
}
try
{
qtyItem4 = int.Parse(this.txtQuantityItem4.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered is not valid" +
"\nPlease try again");
return;
}
}
// Calculate the sub-total for this item
subTotalShirts = qtyShirts * unitPriceShirts;
subTotalPants = qtyPants * unitPricePants;
subTotalItem1 = qtyItem1 * unitPriceItem1;
subTotalItem2 = qtyItem2 * unitPriceItem2;
subTotalItem3 = qtyItem3 * unitPriceItem3;
subTotalItem4 = qtyItem4 * unitPriceItem4;
// Calculate the total based on sub-totals
cleaningTotal = subTotalShirts + subTotalPants + subTotalItem1 +
subTotalItem2 + subTotalItem3 + subTotalItem4;
taxRate = decimal.Parse(this.txtTaxRate.Text);
// Calculate the amount owed for the taxes
taxAmount = cleaningTotal * taxRate / 100;
// Add the tax amount to the total order
netPrice = cleaningTotal + taxAmount;
// Display the sub-total in the corresponding text box
txtSubTotalShirts.Text = subTotalShirts.ToString("F");
txtSubTotalPants.Text = subTotalPants.ToString("F");
txtSubTotalItem1.Text = subTotalItem1.ToString("F");
txtSubTotalItem2.Text = subTotalItem2.ToString("F");
txtSubTotalItem3.Text = subTotalItem3.ToString("F");
txtSubTotalItem4.Text = subTotalItem4.ToString("F");
txtCleaningTotal.Text = cleaningTotal.ToString("F");
txtTaxAmount.Text = taxAmount.ToString("F");
txtNetPrice.Text = netPrice.ToString("F");
}private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
|
Using a Custom Format |
By default, the time displays using the H:M:SS AM/PM format. This means that the time uses 1 digit for the hours from 0 to 9, 1 digit for the minutes from 0 to 9, 1 digit for the seconds from 0 to 9 and the AM or PM for morning or afternoon. To customize the way the time displays, first set the Format property to Custom. Then, in the CustomFormat property, use a combination of the following characters to create a custom format:
| Format | Used For | Description |
| h | Hour for 12-hour basis | Used to display the hour with one digit if the value is less than 10 |
| hh | Hour for 12-hour basis | Used to display the hour with a leading 0 if the value is less than 10 |
| H | Hour for 24-hour basis | Used to display the hour with one digit if the value is less than 10 |
| HH | Hour for 24-hour basis | Used to display the hour with a leading 0 if the value is less than 10 |
| m | Minute | Used to display the minute with one digit if the value is less than 10 |
| mm | Minute | Used to display the minute with a leading 0 if the value is less than 10 |
| t | AM/PM | Displays the letter A or P for the AM or PM section |
| tt | AM/PM | Displays the letters AM or PM for the last section |
You can set the format at design time using the Format field on the Properties window. To set the format at run time, assign the desired format to the DateTimePicker.CustomFormat property.
By default, after adding the control to the form or container, it assumes the time of the computer when the control was added. If you want to set a different time, apply a Format combination to the Value property. In the same way, at any time, you can retrieve the time value on the control by accessing the Value property.
|
|
||
| Previous | Copyright © 2004-2008 FunctionX, Inc. | Home |
|
|
||