Home

Using a Month Calendar Control

 

Selecting a Date

As mentioned in our description, the calendar control displays the days of a selected month. The control also displays the remaining days, if any, of the first week of the currently selected month; that is, the days of the previous month that share the week with the first day of the first week of the selected month. The control also displays the first days of the subsequent month that share the week with the last day of the current month.

To use the calendar control, the user can click a date, whether it a date from the current month or a day of the other (previous and next) month. When the user has clicked a date to select it, the control fires a DateSelected event. The DateSelected event is of type DateRangeEventArgs.

The user can also select a date using the keyboard. To do this, the user must first give focus to the control. This is possible by pressing Tab continuously until the control receives focus (or by clicking any date on the control. To select a date using the keyboard, the user can continually press one of the arrow keys (on the keyboard) until the desired date is selected.

You too can programmatically select a date on the calendar control. To do this, assign a valid DateTime value to both the SelectionStart and the SelectionEnd properties. Here is an example:

private void btnSelect_Click(object sender, EventArgs e)
{        
        monthCalendar1.SelectionStart = new DateTime(1988, 12, 5);
        monthCalendar1.SelectionEnd = new DateTime(1988, 12, 5);
}

When a date has been selected, whether by the user (using the mouse or the keyboard) or by you (through code), the control fires a DateChanged event. The DateChanged event is of type DateRangeEventArgs.

The DateRangeEventArgs class is equipped with two properties: Start and End. When the user clicks a date, these two properties hold the date that was clicked. This means that you can use either of these properties to know the date that was clicked. Both the Start and the End properties are of type DateTime.

Practical LearningPractical Learning: Showing Two Months

  1. On the form, click the calendar control
  2. In the Properties window, click the Events button and double-click DateSelected
  3. Implement the event as follows:
     
    private void calTimeFrame_DateSelected(object sender,
                DateRangeEventArgs e)
    {
            DateTime DateStart = e.Start;
            if (DateStart.DayOfWeek != DayOfWeek.Monday)
        	MessageBox.Show("The first day of your time frame must be a Monday");
    }
  4. Execute the application and try selecting a range of dates
  5. Close the form and return to your programming environment

The Date Range Selection

When the user clicks the calendar control, one date is selected. As mentioned in our description, you can give the control the ability to display more than one month. To make this possible, when creating the control, set its width to have enough space. In the same way, you can increase the height to display many months.

To select more than one date, the user can click one date, hold the mouse down, and drag to the left, the right, up or down:

Calendar

The user can also select a range of dates using the keyboard or using a combination of the mouse and the keyboard. To do this, the user must first give focus to the control. To select a range of dates using the keyboard, the user can press and hold Shift, then press one of the arrow keys continually until the last date of the desired range is selected (in reality, we will see that there is a property that controls the maximum range of dates that can be selected). To select a range of dates using a combination of the mouse and keyboard, the user can click the first date, press and hold Shift, then click the last date.

After selecting the days, the starting date is stored in the SelectionStart property. The last date of the selection is stored in the SelectionEnd property. Both properties are of DateTime type. The range of the selected dates is stored in a SelectionRange value. SelectionRange is simply a class that can give you information about the beginning and the end of a selected range of dates.

To programmatically select a date, assign the starting to the SelectionStart property and assign the last date to the SelectionEnd property.

By default, the user can select only up to 7 days at a time. If you want the user to be able to select more or less days than that. Here is an example where the user have selected dates from the 11 to the 20th, that is 10 days:

If you configure the control to display more than one month, the user can select days from one month to another as long as the days are in a range. Here is an example of a calendar that displays two months and the user had selected dates from September 21st, 2007 to October 11th, 2007:

After selecting a range of dates, the control fires a DateChanged event, which is of type DateRangeEventArgs. We saw earlier that the DateRangeEventArgs class has two properties. The DateRangeEventArgs.Start property holds the starting date of the range that the user made. The DateRangeEventArgs.End holds the last date from the range that the user made.

Practical LearningPractical Learning: Showing Two Months

  1. On the form, click the calendar control and drag its right border so it can display two months:
     
  2. Right-click the form and click View Code
  3. Change the DateSelected event as follows:
     
    void EnableForInvalidDate(bool Enable)
    {
        if (Enable == true)
        {
            txtMonday1.Enabled    = true; txtMonday2.Enabled    = true;
            txtTuesday1.Enabled   = true; txtTuesday2.Enabled   = true;
            txtWednesday1.Enabled = true; txtWednesday2.Enabled = true;
            txtThursday1.Enabled  = true; txtThursday2.Enabled  = true;
            txtFriday1.Enabled    = true; txtFriday2.Enabled    = true;
            txtSaturday1.Enabled  = true; txtSaturday2.Enabled  = true;
            txtSunday1.Enabled    = true; txtSunday2.Enabled    = true;
            txtRegularTime.Enabled = true; txtRegularAmount.Enabled = true;
            txtOvertime.Enabled = true; txtOvertimeAmount.Enabled = true;
            txtNetPay.Enabled = true; btnCalculate.Enabled = true;
        }
        else if (Enable == false)
        {
            txtMonday1.Enabled = false; txtMonday2.Enabled = false;
            txtTuesday1.Enabled = false; txtTuesday2.Enabled = false;
            txtWednesday1.Enabled = false; txtWednesday2.Enabled = false;
            txtThursday1.Enabled = false; txtThursday2.Enabled = false;
            txtFriday1.Enabled = false; txtFriday2.Enabled = false;
            txtSaturday1.Enabled = false; txtSaturday2.Enabled = false;
            txtSunday1.Enabled = false; txtSunday2.Enabled = false;
            txtRegularTime.Enabled = false; txtRegularAmount.Enabled = false;
            txtOvertime.Enabled = false; txtOvertimeAmount.Enabled = false;
            txtNetPay.Enabled = false; btnCalculate.Enabled = false;
        }
    }
    
    private void calTimeFrame_DateSelected(object sender,
                DateRangeEventArgs e)
    {
        DateTime DateStart = e.Start;
        // Each payroll will cover 2 weeks
        // This application assumes that the company started 
        // implementing the time sheet on January 1st, 2007
        // Our week day will go from Monday of the first week
        // to Sunday of the following week
        // The first date of the time frame must be a Monday
        if (DateStart.DayOfWeek != DayOfWeek.Monday)
        {
            MessageBox.Show("The first day of your time frame must be a Monday");
            EnableForInvalidDate(false);
            return;
        }
        else
            EnableForInvalidDate(true);
    
        // Now that the user has selected a date that starts on Monday,
        // We will check that it corresponds to 
        // 2 weeks by 2 weeks after January 1st, 2007
        // To start, we must get the difference of days between 
        // the selected starting date and January 1st, 2007
        TimeSpan tmDifference = DateStart.Subtract(new DateTime(2007, 1, 1));
        int days = tmDifference.Days;
        // Now that we have the number of days,
        // this number must be divisible by 14 (2 weeks)
        if ((days % 14) != 0)
        {
            MessageBox.Show("Invalid starting period - Please try again");
            EnableForInvalidDate(false);
            return;
        }
        else
            EnableForInvalidDate(true);
    
        // Now that we have a valid starting period, 
        // let's help the user and select the time period
        DateTime range = e.Start.AddDays(-1);
        calTimeFrame.SelectionEnd = range.AddDays(14);
    }
  4. Return to the form and double-click the Close button
  5. Implement its Click event as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
                Close();
    }
  6. Execute the application and try selecting a range of dates
  7. Close the form and return to your programming environment

The Maximum Date Range Selection

To control the number of days you want the user to be able to select, change the value of the MaxSelectionCount property. The user cannot select more days than the MaxSelectionCount value but the user can select less.

Practical LearningPractical Learning: Set the Maximum Range

  1. Return to the form and click the calendar control
  2. In the Properties window, click MaxSelectionCount, type 14, and press Enter
  3. Save the form
 

Previous Copyright © 2007 FunctionX, Inc. Next