Windows Forms 2.0 – DateTimePicker Control Recipes
The System.Windows.Forms.DateTimePicker control allows the user to select a date and time. It gives a user the ability to select a single item from a list of dates or times and to display that date/time in a specified format.
How Do I Get Started With The DateTimePicker Control
The control's MaxDate and MinDate properties determine the range of dates and times that can be selected. The Format property sets the DateTimePickerFormat of the control. The CustomFormat property lets you create your own format style.
The following code example creates an new instance of a DateTimePicker control and initializes it. The control’s MinDate, MaxDate and CustomFormar properties are set.
private void CreateDateTimePicker()
{
// Create a new DateTimePicker control and initialize it.
DateTimePicker dtp = new DateTimePicker();
dtp.Location = new Point(30, 30);
// Set the MinDate and MaxDate.
dtp.MinDate = new DateTime(1990, 1, 1);
dtp.MaxDate = DateTime.Today;
// Set the CustomFormat string.
dtp.CustomFormat = "MMMM dd, yyyy - dddd";
dtp.Format = DateTimePickerFormat.Custom;
// To display the time with the DateTimePicker control
// dtp.Format = DateTimePickerFormat.Time;
// Show the CheckBox and display the control as an up-down control.
// dtp.ShowCheckBox = true;
// dtp.ShowUpDown = true;
Controls.Add(dtp);
}
Note : The DateTimePicker control only supports Gregorian calendars.
How Do I Set A Blank Date In The DateTimePicker Control
There could be a situation where you need to use the datetimepicker on a form and keep its date as blank, until the user decides to enter a date by clicking on the drop down box. This would give a user the flexibility to not select a date.
To display a null value in the DateTimePicker, set its custom property to an empty string and then set the Format property to DateTimePickerFormat.Custom.
dtp.CustomFormat = " ";
//set the format to custom
dtp.Format = DateTimePickerFormat.Custom;
After a date selection, if you want the values to be displayed, you have to reset the DateTimePickerFormat to Long or Short as shown here :
dtp.Format = DateTimePickerFormat.Short;
The following code example contains the entire code to test this feature. The example required a Button (btnNullDate) to be placed on the form.
In the CreateDateTimePicker(), add the following line of code:
...
Controls.Add(dtp);
dtp.ValueChanged+= new EventHandler(dtp_ValueChanged);
...
private void btnNullDate_Click(object sender, EventArgs e)
{
//set the format to custom
dtp.Format = DateTimePickerFormat.Custom;
// set custom format to empty
dtp.CustomFormat = " ";
}
private void dtp_ValueChanged(object sender, EventArgs e)
{
dtp.Format = DateTimePickerFormat.Short;
}
How Do I Programmatically DropDown The DateTimePicker Control
The System.Windows.Forms.SendKeys class provides methods of sending keystrokes and keystrokes combinations to the active application. SendKeys,SendWait() method sends the given Keys to the active application and waits for the message to be processed.
Each key is represented by one or more characters. The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses () have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}).
The following code example demonstrates the usage of SendKeys class to programmatically dropdown the DateTimePicker Control on the click of a button ()
private void btnDropDown_Click(object sender, EventArgs e)
{
dtp.Focus();
SendKeys.SendWait("%{DOWN}");
}
private void dtp_DropDown(object sender, System.EventArgs e)
{
System.Text.StringBuilder messageBuilder =
new System.Text.StringBuilder();
messageBuilder.Append("DropDown Event Programatically Invoked");
MessageBox.Show(messageBuilder.ToString());
}
Conclusion :
In this article, we saw a recipe approach to performing some of the most common tasks with the DateTimePicker Control. I hope the article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn