Calendar Controls

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 8.  Using Controls


VB.NET supports two types of calendar controls: the MonthCalendar and the DateTimePicker . Each allows the user to select a date or time from a graphical control. They should be used instead of textboxes whenever possible because they reduce the chance of data entry errors for dates (such as entering February 31).

DateTimePicker

The DateTimePicker control allows the user to select a date and/or time from a graphical control. The format of the date/time displayed can be controlled using the Format property. The properties MinDate and MaxDate can be used to specify a limit for the dates shown. The Value property contains the selected date/time. Additional properties control the appearance of the control.

In the directory for this chapter, we have several versions of our case study. Each version will use specific controls to improve upon the appearance and/or ease of use of the application. BetterAcmeGui uses a DateTimePicker to select the check-in date for customer reservations .

We began by replacing the textbox for the check-in date with a DateTimePicker named dtCheckinDate . In the new reservation form's Load event, we placed the following code so that the check-in date defaults to the current date, reservations cannot be made for past dates and are only allowable up to one year in advance. We use the DateAdd function to calculate the date one year from Now .

 graphics/codeexample.gif Private Sub NewReservationForm_Load(ByVal sender As _  System.Object, ByVal e As System.EventArgs) _  Handles MyBase.Load  dtCheckinDate.Value = Date.Now   dtCheckinDate.MinDate = Date.Now   dtCheckinDate.MaxDate = _   DateAdd(DateInterval.Year, 1, Date.Now)  End Sub 

The CheckInDate property of the NewReservationForm was modified to reference to the DateTimePicker control.

 Public Property CheckInDate() As Date    Get       Return Convert  .ToDateTime(dtCheckinDate.Value)  End Get    Set(ByVal Value As Date)  dtCheckinDate.Value  = Value.ToString()    End Set End Property 

Figure 8-11 shows the reservation dialog before and after the dropdown box was selected. The advantage of using this control is that the user must enter a valid date; we do not have to perform any validation on the check-in date.

Figure 8-11. Using the DateTimePicker control.

graphics/08fig11.jpg

MonthCalendar

The MonthCalendar control allows a user to select a date, or range of dates, from a graphical calendar. You should examine the MSDN to see some of the neat features it offers. These include the following properties:

  • FirstDayOfWeek indicates the first day of the week that is displayed.

  • ShowTodayCircle indicates whether the current day is circled.

  • ShowWeekNumbers indicates whether the week number is shown next to each week.

  • AnnuallyBoldedDates is an array of Date objects that represents the dates that are bolded every year.

  • MonthlyBoldedDates is an array of Date objects that represents the dates that are bolded every month.

  • BoldedDates is an array of Date objects that represents specific dates that are bolded.

graphics/codeexample.gif

The following simple program, found in BirthdayGreetings , displays a birthday greeting to Mary when March 6th is clicked. The program also bolds January 9th each year. Figure 8-12 shows two snapshots of the program. In the top left version, March 6th was clicked. In the bottom right version, you can see that January 1 was clicked and January 9 is bolded.

Figure 8-12. Using the MonthCalendar control.

graphics/08fig12.jpg

Step 1: Setting up Annually Bolded Dates

When we set annually bolded dates, we must provide valid dates. This means that the date(s) provided must include a year. However, the year is ignored when bolding annual dates.

 Private Sub BirthdayGreetings_Load(ByVal sender As _  System.Object, ByVal e As System.EventArgs) _  Handles MyBase.Load  calBirthday.AddAnnuallyBoldedDate(#1/9/1988.htm#)  End Sub 

Step 2: Programmatically Setting the Date

Our btnJump's Click event handler must adjust the calendar date to January 1 of the year entered. We do this using the calendar's SetDate method. The DateSerial method is used to build a date from integer values representing year, month, and day.

 Private Sub btnJump_Click(ByVal sender As Object, _  ByVal e As System.EventArgs) Handles btnJump.Click  calBirthday.SetDate(DateSerial(txtYear.Text, 1, 1))   txtYear.Text = ""  End Sub 

Step 3: Determining the Selected Date

The calendar's DateSelected event is generated whenever a date on the calendar is selected (by the user or programmatically). We will handle this event in order to determine whether we should wish Mary a "happy birthday."

 Private Sub calBirthday_DateSelected(_  ByVal sender As Object, _  ByVal e As System.Windows.Forms.DateRangeEventArgs) _  Handles calBirthday.DateSelected  If calBirthday.SelectionStart.Month = 3 And _   calBirthday.SelectionStart.Day = 6 Then   MessageBox.Show("Happy Birthday, Mary", "Birthday", _   MessageBoxButtons.OK, _   MessageBoxIcon.Exclamation)   End If  End Sub 

Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net