DateTimePicker Control

Table of contents:

The DateTimePicker control (see output of Fig. 14.11) is similar to the MonthCalendar control, but displays the calendar when a down arrow is selected. The DateTimePicker can be used to retrieve date and time information from the user. The DateTimePicker is also more customizable than a MonthCalendar controlmore properties are provided to edit the look and feel of the drop-down calendar. Property Format specifies the user's selection options using the DateTimePickerFormat enumeration. The values in this enumeration are Long (displays the date in long format, as in Friday, July 1, 2005), Short (displays the date in short format, as in 7/1/2005), Time (displays a time value, as in 11:48:02 PM) and Custom (indicates that a custom format will be used). If value Custom is used, the display in the DateTimePicker is specified using property CustomFormat. The default event for this control is ValueChanged, which occurs when the selected value (whether a date or a time) is changed. DateTimePicker properties and a common event are summarized in Fig. 14.10.

Figure 14.10. DateTimePicker properties and an event.

(This item is displayed on page 661 in the print version)

DateTimePicker properties and an event


DateTimePicker Properties


Sets the text color for the calendar.


Sets the calendar's background color.


Sets the custom format string for the user's options.


Sets the format of the date and/or time used for the user's options.


The maximum date and time that can be selected.


The minimum date and time that can be selected.


Indicates if a CheckBox should be displayed to the left of the selected date and time.


Used to indicate that the control should have up and down Buttons. This is helpful for instances when the DateTimePicker is used to select a timethe Buttons can be used to increase or decrease hour, minute and second values.


The data selected by the user.

Common DateTimePicker Event


Generated when the Value property changes, including when the user selects a new date or time.

Figure 14.11 demonstrates using the DateTimePicker control to select an item's drop-off time. Many companies use such functionality. For instance, several online DVD rental companies specify the day a movie is sent out, and the estimated time that the movie will arrive at your home. In this application, the user selects a drop-off day, and then an estimated arrival date is displayed. The date is always two days after drop off, three days if a Sunday is reached (mail is not delivered on Sunday).

Figure 14.11. Demonstrating DateTimePicker.

 1 // Fig. 14.11: DateTimePickerForm.cs
 2 // Using a DateTimePicker to select a drop off time.
 3 using System;
 4 using System.Windows.Forms;
 6 public partial class DateTimePickerForm : Form
 7 {
 8 // default constructor
 9 public DateTimePickerForm()
10 {
11 InitializeComponent();
12 } // end constructor
14 private void dateTimePickerDropOff_ValueChanged( 15 object sender, EventArgs e ) 16 { 17 DateTime dropOffDate = dateTimePickerDropOff.Value; 18 19 // add extra time when items are dropped off around Sunday 20 if ( dropOffDate.DayOfWeek == DayOfWeek.Friday || 21 dropOffDate.DayOfWeek == DayOfWeek.Saturday || 22 dropOffDate.DayOfWeek == DayOfWeek.Sunday ) 23 24 //estimate three days for delivery 25 outputLabel.Text = dropOffDate.AddDays( 3 ).ToLongDateString(); 26 else 27 // otherwise estimate only two days for delivery 28 outputLabel.Text = dropOffDate.AddDays( 2 ).ToLongDateString(); 29 } // end method dateTimePickerDropOff_ValueChanged 30 31 private void DateTimePickerForm_Load( object sender, EventArgs e ) 32 { 33 // user cannot select days before today 34 dateTimePickerDropOff.MinDate = DateTime.Today; 35 36 // user can only select days of this year 37 dateTimePickerDropOff.MaxDate = DateTime.Today.AddYears( 1 ); 38 } // end method DateTimePickerForm_Load 39 } // end class DateTimePickerForm





The DateTimePicker (dateTimePickerDropOff) has its Format property set to Long, so the user can select a date and not a time in this application. When the user selects a date, the ValueChanged event occurs. The event handler for this event (lines 1429) first retrieves the selected date from the DateTimePicker's Value property (line 17). Lines 2022 use the DateTime structure's DayOfWeek property to determine the day of the week on which the selected date falls. The day values are represented using the DayOfWeek enumeration. Lines 25 and 28 use DateTime's AddDays method to increase the date by two days or three days, respectively. The resulting date is then displayed in Long format using method ToLongDateString.

In this application, we do not want the user to be able to select a drop-off day before the current day, or one that is more than a year into the future. To enforce this, we set the DateTimePicker's MinDate and MaxDate properties when the Form is loaded (lines 34 and 37). Property Today returns the current day, and method AddYears (with an argument of 1) is used to specify a date one year in the future.

Let's take a closer look at the output. This application begins by displaying the current date (Fig. 14.11(a)). In Fig. 14.11(b), we selected the 12th of July. In Fig. 14.11(c), the estimated arrival date is displayed as the 14th. Figure 14.11(d) shows that the 12th, after it is selected, is highlighted in the calendar.

LinkLabel Control



    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look


    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2


    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures



    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types


    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: