Using a Month Calendar Control
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…
Selecting a Date#
The calendar control primarily shows the days within a chosen month. It also includes the trailing days from the previous month that belong to the first week of the selected month, as well as the initial days of the following month that fall into the final week of the current month. Users interact with the control by clicking any visible date, regardless of which month it belongs to. Selecting a date triggers the DateSelected event, which utilizes DateRangeEventArgs. Alternatively, users can navigate the calendar with a keyboard by first focusing on the control (often via the Tab key or a mouse click) and then using the arrow keys to move through dates. Developers can also pick a date through code by setting both the SelectionStart and the SelectionEnd properties to a specific DateTime value, as shown here:
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 Learning: Showing Two Months
- On the form, click the calendar control
- In the Properties window, click the Events button and double-click DateSelected
- 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"); }
- Execute the application and try selecting a range of dates
- 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:
A range of dates can be selected using the keyboard or a mix of mouse and keyboard inputs. First, the control must have focus. For keyboard selection, hold down the Shift key while using the arrow keys to extend the selection to the desired end date (note that a specific property limits how many days can be selected at once). To use both, click the start date, hold Shift, and then click the end date. The beginning of the selection is kept in the SelectionStart property, while the conclusion is in the SelectionEnd property; both are DateTime objects. This range is represented by a SelectionRange class, which provides details about the start and end points. You can set these programmatically by assigning values to SelectionStart and SelectionEnd. By default, selections are limited to 7 days, though this can be adjusted. Below is an illustration of a 10-day selection ranging from the 11th to the 20th:
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 Learning: Showing Two Months
- On the form, click the calendar control and drag its right border so it can display two months:
- Right-click the form and click View Code
- 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); }
- Return to the form and double-click the Close button
- Implement its Click event as follows: private void btnClose_Click(object sender, EventArgs e) { Close(); }
- Execute the application and try selecting a range of dates
- Close the form and return to your programming environment
The Maximum Date Range Selection#
You can adjust the MaxSelectionCount property to limit how many days a user is permitted to select at once. While users can choose fewer days than this limit, they are prevented from exceeding it. Practical Learning: Configuring the Maximum Selection Range
- Return to the form and click the calendar control
- In the Properties window, click MaxSelectionCount, type 14 , and press Enter
- Save the form
Previous Copyright © 2008 C# Key Next

