You want to add controls to a form to let the user enter or select a date.
Sample code folder: Chapter 07\DateEntry
Use a text box for easy text entry, a DateTimePicker for a control more tailored to entering a date, or a MonthCalendar control for a more graphical way to allow the user to select a date.
The sample code in this recipe presents a form with all three controls, each of which has its uses, advantages, and drawbacks. Experiment with them to determine which will work best for your goals.
Figure 7-18 shows the form during development, with the three date-entry controls and three associated Label controls. As the following code listing shows, changes to the dates in each control are shown in the label control to its right.
Figure 7-18. Three different ways for a user to enter or select a date
The TextBox control is the simplest in the sense that no special property settings are required to define its behavior as a field for entering dates. Instead, most of the work is done in its TextChanged event:
Private Sub TextBox1_TextChanged( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles TextBox1.TextChanged ' ----- Check and display only valid dates. If (IsDate(TextBox1.Text) = True) Then Label1.Text = Date.Parse(TextBox1.Text).ToShortDateString Else Label1.Text = "" End If End Sub
This event activates whenever any change is made to the TextBox's text. During date entry the incomplete string in this text box will probably not represent a valid date, so the IsDate() function verifies the entered text before use. If it's not yet a valid date, Label1 displays nothing, but as soon as the text becomes a valid date, the string is parsed, and the date is reformatted for display in Label1.
The DateTimePicker control does have some properties you can use to control the interaction with the user. For example, in this demonstration the control's ShowUpDown property has been set to true to show the little arrows at the end of the field for incrementing and decrementing the displayed date. The control's Format property has also been set to Short to display the date in a simplified format.
At runtime, the DateTimePicker control allows the user to highlight one of the three parts of the dateyear, month, or dayand then use the up and down arrows to scroll through possible values for each. The control's ValueChanged event activates as the user does so, and the current date is displayed in Label2. Here's the single line of code added to this event to cause this action:
Private Sub DateTimePicker1_ValueChanged( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles DateTimePicker1.ValueChanged ' ----- Show the selected date. Label2.Text = DateTimePicker1.Value.ToShortDateString End Sub
The MonthCalendar control provides the user with an even more interactive and graphical way to select a date. When any date on the displayed calendar is clicked, the control's DateChanged event fires, and the line of code in this event handler causes Label3 to update with the currently selected date:
Private Sub MonthCalendar1_DateChanged( _ ByVal sender As System.Object, _ ByVal e As System.Windows. Forms.DateRangeEventArgs) _ Handles MonthCalendar1.DateChanged ' ---- Show the slected date. Label3.Text = _ MonthCalendar1.SelectionStart.ToShortDateString End Sub
The TextBox control's text needs to be parsed to become a Date value, but the DateTimePicker and MonthCalendar controls' Value and SelectionStart properties return Date values directly.
Figure 7-19 shows the form in action as a user is selecting a date using the MonthCalendar control.
Figure 7-19. The MonthCalendar control sports a variety of interactive features when selecting a date