DateTimePicker in Visual Basic .NET
Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013
This lesson describes the DateTimePicker control
This control allows you to select a date and time and can display one of several styles, depending on its property values.
If you set the ShowUpDown to False, it will display a drop-down arrow on its right which if clicked, displays a calendar. The right and left arrows at the top of the calendar let you move through months, and if you click the calendar's month, the control displays a pop-up menu listing the months so that you can quickly select one. Also if you click the year, the control displays small up and down arrows that you can use to change the year. Once you set the month and year, you can click a date to select it and close the calendar.
If you set the ShowUpDown property to True, you can click a date field (month, date or year) and then use the up and down arrow keys to adjust the date value.
To have the DataTimePicker value displayed in a specific way, you use Format property which actually determines the way in which the control displays dates and times. This property can take the values Long, Short, Time, and Custom (the results depend on the regional settings on the computer as well).
For example Short with Time settings in US will result to 05/31/2014
If you set the Format property to Custom, the control uses the date and time format string in the CustomFormat property e.g. MM/dd/yyyy will result to the same as Short which is 05/31/2014
To set the appearance of the calendar, you may use the Appearance properties like CalendarFont, CalendarForeColor, CalendarMonthBackground, CalendarTitleBackColor, CalendarTitleForeColor, and CalendarTrailingForeColor.
Finally to get or set the control's date and time, you use the Value property.
' set the value to today's date Today_Dtp.Value = DateTime.Now ' get the datatimepicker value example Dim age As Integer = DateDiff(DateInterval.Year, Birthday_Dtp.Value, Today_Dtp.Value) ' check the result MessageBox.Show("You are " & age.ToString & " years old.")Previous Lesson | Next Lesson