DateTimePicker

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
70-505 Study Guide
70-526 Study Guide

1. Forms Controls

2. Integrating Data
3. Printing/Reporting
4. Enhancing Usability
5. Asynchronous Prog.
6. Forms Controls
7. Configure/Deploy

edit
Exam 70-526 Preparation Guide: Add and configure date-setting controls on a Windows Form


Contents


DateTimePicker is ideal for choosing a single date amd/or time value and requires the same amount of space as an ordinary drop-down list box. When the user clicks the drop-down button, a month calendar appears. The operation of the control from this point is exactly the same as the MonthCalendar control.

Image:526_111.jpg


Inheritance hierarchy

System.Object 
  System.MarshalByRefObject 
    System.ComponentModel.Component 
      System.Windows.Forms.Control 
        System.Windows.Forms.DateTimePicker


Useful calendar properties

  • CalendarFont - Determines the font style applied to the calendar.
  • CalendarForeColor - GeDetermines the foreground color of the calendar.
  • CalendarMonthBackground - Determines the background color of the calendar month.
  • CalendarTitleBackColor - Determines the background color of the calendar title.
  • CalendarTitleForeColor - Determines the foreground color of the calendar title.
  • CalendarTrailingForeColor - Determines the foreground color of the calendar trailing dates.


Useful properties

  • BackColor - Indicates the background color of the DateTimePicker control.
  • CustomFormat - Determines the custom date/time format string.
  • DropDownAlign - Determines the alignment of the drop-down calendar on the DateTimePicker control. Takes a value of either Left or Right.
  • ForeColor - Indicates the foreground color of the DateTimePicker control.
  • Format - Determines the format of the date and time displayed in the control.
  • MaxDate - Determines the maximum date and time that can be selected in the control.
  • MaximumDateTime - Gets the maximum date value allowed for the DateTimePicker control.
  • MinDate - Determines the minimum date and time that can be selected in the control.
  • MinimumDateTime - Gets the minimum date value allowed for the DateTimePicker control.
  • ShowCheckBox - Indicates whether a check box is displayed to the left of the selected date.
  • ShowUpDown - Indicates whether a spin button control (also known as an up-down control) is used to adjust the date/time value.
  • Value - Determines the date/time value assigned to the control.


Adding a DateTimePicker manually

You can add a DateTimePicker to a form at run time in the following manner:

private void AddDateTimePicker()
{
  DateTimePicker DateTimePicker1 = new DateTimePicker();
 
  //Set size and location.
  DateTimePicker1.Location = new Point(40, 88);
  DateTimePicker1.Size = new Size(160, 21);
    
  // Set the alignment of the drop-down MonthCalendar to right.
  DateTimePicker1.DropDownAlign = LeftRightAlignment.Right;
 
  // Set the Value property to 50 years before today.
  DateTimePicker1.Value = System.DateTime.Now.AddYears(-50);
 
  //Set a custom format containing the string "of the year"
  DateTimePicker1.Format = DateTimePickerFormat.Custom;
  DateTimePicker1.CustomFormat = "MMM dd, 'of the year' yyyy ";
 
  // Add the DateTimePicker to the form.
  this.Controls.Add(DateTimePicker1);
}


Customizing a DateTimePicker

In the above sample, the Format of the DateTimePicker control is set to DateTimePickerFormat.Custom. Then in the next line, a CustomFormat string is applied.

When the Format property is set to DateTimePickerFormat.Custom, a custom display setting for the DateTimePicker control can be used. The CustomFormat property contains the string value to use which may include a number of format codes. These codes are managed by the sealed DateTimeFormatInfo class. The following table shows a number of these codes, along with some corresponding properties in the DateTimeFormatInfo class.

Pattern Description Default U.S. English Values DateTimeFormatInfo Property
d Day of the month. 1 to 31
dd Two-digit day of the month. 01 to 31
ddd Abbreviated day of the week. Sun to Sat AbbreviatedDayNames
dddd Full day of the week. Sunday to Saturday DayNames
M Numeric month. 1 to 12
MM Two-digit numeric month. 01 to 12
MMM Abbreviated month name. Jan to Dec AbbreviatedMonthNames
MMMM Full month name. January to December MonthNames
y Year without century. 1 to 99
yy Two-digit year without century. 01 to 99
yyyy Four-digit century. 0001 to 9999
gg Period or era, if any. B.C. or A.D.
h Hour on a 12-hour clock. 1 to 12
hh Two-digit hour on a 12-hour clock. 01 to 12
H Hour on a 24-hour clock. 1 to 24
HH Two-digit hour on a 24-hour clock. 01 to 24
m Minute. 0 to 59
mm Two-digit minute. 00 to 59
s Second. 0 to 59
ss Two-digit second. 00 to 59
tt AM/PM designator. AM or PM AMDesignator and PMDesignator
 : Default time separator.  : ( a colon) TimeSeparator
/ Default date separator. / (a slash) DateSeparator
ā€˜c’ Displays the specified character. For example, ā€˜s’ will display the character s rather than the number of seconds.


Spinning the control

To use a spin button control (also known as an up-down control) to adjust the date/time value, set the ShowUpDown property to true. The calendar control will not drop down when the control is selected. The date and time can be adjusted by selecting each element individually and using the up and down buttons to change the value.

Image:526_112.jpg


Showing the time

The time can be shown by setting the Format property to Time.


MSDN references


Previous_Page_.gif Next_Page_.gif
© 2007-2008 Mike Kitchen

Today's Deals: Electronics

Personal tools