DotNetCurry Logo

Windows Forms 2.0 – DateTimePicker Control Recipes

Posted by: Suprotim Agarwal , on 3/17/2007, in Category WinForms & WinRT
Views: 40625
Abstract: In this article, we will explore how to perform some common tasks using the DateTimePicker Control of Windows Forms 2.0.
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.
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+
Further Reading - Articles You May Like!
Author
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 a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by OIT on Wednesday, September 19, 2007 5:52 AM
If I set blank date in DateTimePicker, I can't see pointer when I focus to it
Comment posted by Suprotim on Thursday, September 20, 2007 7:25 AM
Are you referring to a tooltip!! Well it does not show a tooltip as it can not receive any message or get any event on MouseHover. To workaround, capture the MouseMove event of the form and check if it is pointing at the DTP. If  it is, set the Form's TooTip to a desired value.
Comment posted by Ameet Kaur Sira on Thursday, June 5, 2008 9:56 AM
Hello,
I want to add datetimepicker in row of flexgrid in vb.net 2005. Will you please help me. Thanks in advance.
Comment posted by vivek on Tuesday, July 15, 2008 3:56 AM
hey,
i want to change the color of my datetimepicker by coding not by properties...
thanks for this one..
reply me soon..
Comment posted by Suprotim Agarwal on Wednesday, July 16, 2008 1:52 PM
Vivek: If you are trying to modify the forecolor of the DateTimePicker, then you cannot as it is embedded to use SystemColors.WindowsText. However what can be done is create your own custom class that inherits from DateTimePicker class and then override the OnPaint event.
Comment posted by Kishor on Saturday, August 16, 2008 12:55 AM
Hi,
I have 4 dateTimePickers in a winForm. Like..dtStartTime, dtEndTime, dtBreakTime, dtWorkTime. I want to get a add 2 control and subtract third and store the value to forth control like:

dtWorkTime = (dtEndTime-dtStartTime)-dtBreakTime

Can anybody help? Pleasee..
Thanks in advance.
Comment posted by Ramon on Wednesday, September 16, 2009 1:07 AM
I Set the CustomFormat to "" and the Format = DateTimePickerFormat.Custom. Nothing happens, it is not true. Any other way to get a blank displayed instead the current date ?
Comment posted by Tarun Nigam on Sunday, May 16, 2010 4:57 AM
How we focus From Day to month after entering day deatils in date time picker
Comment posted by a on Wednesday, March 28, 2012 2:55 AM
a