Xceed Grid for WinForms v4.3 Documentation
WinDatePicker control

Welcome to Xceed Grid for WinForms v4.3 > Basic Concepts > Editor Controls > WinDatePicker control

The WinDatePicker class represents an extensible and themable date picker control for Windows Forms. It supports regional settings, as well as both the Windows XP and classic Windows themes, and can adapt to the current regional settings and Windows theme. 

Textbox area

The WinDatePicker control is a container control which contains an inner textbox. All of the inner textbox's properties and events are accessed via the WinDatePicker control's TextBoxArea. Any properties that are set directly on the WinDatePicker control will also affect any embedded child controls. For example, if the ForeColor property of the WinDatePicker control is set to Color.Green, then the foreground color of all the embedded controls, including the TextBoxArea, will be changed to green. If the ForeColor property of the TextBoxArea is set to Color.Green, then only the foreground color of the TextBoxArea will be changed to green.

Display and edit formats

The format with which the date is displayed is defined by the DisplayFormatProvider and DisplayFormatSpecifier properties. By default, the date will be displayed using the current culture's ShortDatePattern (28/10/2004). To change the formatting pattern used to display the date (by default "d"), the DisplayFormatSpecifier property can be set to any one of the supported formatting patterns and/or characters

The following code demonstrates how to use the DisplayFormatProvider and DisplayFormatSpecifier properties to display a custom formatted date.

VB.NET
Copy Code
Dim datePicker As New WinDatePicker()     

datePicker.DisplayFormatProvider = New System.Globalization.DateTimeFormatInfo()

datePicker.DisplayFormatProvider.FullDateTimePattern = "yyyy -- MMMM -- dddd (dd)"

datePicker.DisplayFormatSpecifier = "F"     

datePicker.Location = New Point( 10, 10 )

Me.Controls.Add( datePicker )
C#
Copy Code
WinDatePicker datePicker = new WinDatePicker();   

datePicker.DisplayFormatProvider = new System.Globalization.DateTimeFormatInfo();

datePicker.DisplayFormatProvider.FullDateTimePattern = "yyyy -- MMMM -- dddd (dd)";

datePicker.DisplayFormatSpecifier = "F";     

datePicker.Location = new Point( 10, 10 );

this.Controls.Add( datePicker );

The format with which the date is edited is defined by the EditFormatProvider and EditFormatSpecifier properties. By default, the date will be edited using the current culture's ShortDatePattern ("d") formatting pattern. To change the formatting pattern used to edit the date (by default "d"), the EditFormatSpecifier property can be set to any one of the supported formatting patterns and/or characters listed below. 

Format Character Format Pattern
d d, dd, ddd, dddd
M M, MM, MMM, MMMM
y y, yy, yyy, yyyy
The following code demonstrates how to use the EditFormatProvider and EditFormatSpecifier properties to edit a custom formatted date.

VB.NET
Copy Code
Dim datePicker As New WinDatePicker()

datePicker.EditFormatProvider = New System.Globalization.DateTimeFormatInfo()

datePicker.EditFormatProvider.ShortDatePattern = "yyyy -- MMMM -- ddd"

datePicker.EditFormatSpecifier = "d"     

datePicker.Location = New Point( 10, 10 )

Me.Controls.Add( datePicker )
C#
Copy Code
WinDatePicker datePicker = new WinDatePicker();

datePicker.EditFormatProvider = new System.Globalization.DateTimeFormatInfo();      

datePicker.EditFormatProvider.ShortDatePattern = "yyyy -- MMMM -- ddd";

datePicker.EditFormatSpecifier = "d";     

datePicker.Location = new Point( 10, 10 );

this.Controls.Add( datePicker );
Notice that the ShortDatePattern of the DateTimeFormatInfo object is set to "yyyy -- MMMM -- ddd"; however the edit date that is used is the equivalent of "yyyy -- MM -- dd". The year (y), month (M), and day (d) format characters and patterns will be modified, if necessary. The following table provides the list of modifications that can occur: 
Format Pattern Set Format Pattern Used
d, dd, ddd, dddd dd
M, MM, MMM, MMMM MM
y, yy yy
yyy, yyyy yyyy
More information on the DateTimeFormatInfo class can be found in the DateTimeFormatInfo class topic in .NET Framework's documentation.

Validation, parsing, and null dates

Every time the focus is removed from the WinDatePicker control's TextBoxArea, the TextBoxArea's Validating event is raised. In the case where the date entered into the TextBoxArea does not match the EditFormatProvider, the Validating event's e.Cancel property will automatically be set to true (indicating that there was an error) and the focus will remain in the TextBoxArea. If the date is valid, e.Cancel will be false, and the focus will be sent to the next control.   The control's Validating event will not be raised if the control's built-in validation has failed or if the extra validation done in the ValidatingText event. 

The TextBoxArea will also raise the ValidatingText event during the Validating event where additional custom text validation can be provided. The e. FullValidation property indicates if the text is currently being validated only against its mask or against all of the validation criteria.   If the custom validation detects that the e. EditText is invalid, e. Valid should be set to false.  The ValidatingText event is only raised when everything "this far" is valid.  It will never be raised if e.Valid is set to false

The WinDatePicker's Value property will always contain the last valid date. If a date is not yet selected, the NullDate and NullDateString properties can be used to display a string (by default, "(null)"). 

The TryParse method can be used to convert a string representation of a date and time to its DateTime equivalent using the formatting information retrieved from the DateTimeFormatInfo object assigned to the EditFormatProvider property.