Visual Studio .NET's DateTimePicker control offers a convenient way to allow users to make a date/time selection. In this quick tip, Irina Medvinskaya creates a sample that uses the DateTimePicker control.
Visual Studio .NET's DateTimePicker control offers a convenient way for users to make a date/time selection. The control allows you to select dates and times and to skip the date format validity of the data since the control lets you limit the input. DateTimePicker is similar to the MonthView control, which allows you to select either a date or a range of dates but doesn't support time selection. In this quick tip, I will create an example that uses the DateTimePicker control.
Find the DateTimePicker control in the Toolbox under the Common Controls section and add it to the Windows Form. Set the following properties:
- Name: dtpDateSelection
- CustomFormat: mm/dd/yyyy
- Format: short
- MaxDate: 12/31/2008
- MinDate: 01/01/2007
- ShowUpDown: False
Add the following code to the Form Load event:
dtpDateSelection.Value = Now
Create the following event:
Private Sub dtpDateSelection_CloseUp(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpDateSelection.CloseUp
ResultIn the first example, the ShowUpDown property is set to False, which means that the control's calendar portion is visible when the user clicks the control. The user then picks the date from the control's calendar portion. Select a date on the DateTimePicker control, and your screen will look similar to Figure A. Now change the ShowUpDown property value to True and run the code again. You will not see the control's calendar portion, and you will be able to pick a date by clicking the up/down arrows of the control. The result will look like Figure B.
Notice that the control will not allow you to pick a date that's smaller than the MinDate property value or larger than the MaxDate property value.
For more information
There are many other properties and important methods you can use to get the DateTimePicker control to behave the way you want. MSDN offers additional details about how to use the DateTimePicker control.
Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a project manager at Citigroup.
————————————————————————————————————————————Get Visual Basic tips in your inbox
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically subscribe today!