Home

Windows Control: The Month Calendar

 

Description

Microsoft Windows provides a control used to select dates on a colorful calendar:

The Month Calendar control

The dates used and the way they display are based on the Regional Settings of the Control Panel. It may also depend on the operating system. This convenient control is called month calendar or simply calendar. The title bar of the control displays two buttons and two labels. The left button allows the user to select the previous month by clicking the button. The left label displays the currently selected month. The right label displays the year of the displayed date. The right button is used to get to the next month.

The calendar can be configured to display more than one month. Here is an example that displays two months:

The Month Calendar control displaying two months

If the control is displaying more than one month, the buttons would increment or decrement by the previous or next month in the list. For example, if the control is displaying April and May and if the user clicks the left button, the control would display March and April. If the control is displaying April and May and the user clicks the right button, the control would display May and June. Also, to select any month of the current year, the user can click the name of the month, which displays the list of months and this allows the user to click the desired month:

The Month Calendar control displaying the list of months

To select a year, the user can click the year number. This changes the year label into a spin button:

The Month Calendar control allowing the user to select a year

To change the year, the user can click the up or down arrows of the spin button. As the spin button is displaying, the user can also use the arrow keys of the keyboard to increase or decrease the value.

Under the title bar, the short names of week days display, using the format set in Control Panel. In US English, the first day is usually Sunday. The first day can be changed by the programmer.

On the control, the currently selected date has a circle around. To select a date on the control, the user clicks the desired date, which changes from the previous selection.

In the main area, the numeric days of the month display on a white background (this color and any color on the control can be changed as we will see in the next section). To select a date, the user clicks it in the list. By default, the calendar opens with today's day circled with a hand-drawn-look-alike ellipse. Using the buttons of the title bar, the month label, and/or the year, the user can change the date. If at one time the calendar is displaying a date other than today, and if the user wants to return to today's date, he or she can click the bottom label that displays Today (you as the programmer can hide the Today label if you want).

Creating a Calendar

To create a calendar control, you can click the MonthCalendar button MonthCalendar in the Toolbox and click the form or the desired container. The MonthCalendar control is based on the MonthCalendar class, which is based on Control. Therefore, to create a calendar control, declare a variable or type MonthCalendar and initialize it appropriately.

Practical LearningPractical Learning: Introducing the Month Calendar Control

  1. Start a new Windows Application named PayrollProcessing2
  2. In the Solution Explorer, right-click Form1.cs and click Rename
  3. Type PayrollProcessing.cs and press Enter
  4. Design the form as follows:
     
    Payroll Processing
    Control Name Text Other Properties
    GroupBox GroupBox   Employee Identification  
    Label Label   &Employee Name:  
    TextBox TextBox txtEmployeeName   AutoCompleteCustomSource Micheline Hammond
    Paul Bertrand Yamaguchi
    Gertrude Monay
    Ernestine Ngaleu
    Andy Barang
    Christophe Yuen
    Jean Michel Kankan

    AutoCompleteSource: CustomSource

    AutoCompleteMode: Accept

     

    Label Label   Hourly &Salary:  
    TextBox TextBox txtHourlySalary    
    GroupBox GroupBox Payroll Time Frame
    MonthCalendar MonthCalendar calTimeFrame Cursor: Hand
    GroupBox GroupBox   Time Values  
    Label Label   Monday  
    Label Label   Tuesday  
    Label Label   Wednesday  
    Label Label   Thursday  
    Label Label   Friday  
    Label Label   Saturday  
    Label Label   Sunday  
    Label Label   First Week:  
    TextBox TextBox txtMonday1 0.00 TextAlign: Right
    TextBox TextBox txtTuesday1 0.00 TextAlign: Right
    TextBox TextBox txtWednesday1 0.00 TextAlign: Right
    TextBox TextBox txtThursday1 0.00 TextAlign: Right
    TextBox TextBox txtFriday1 0.00 TextAlign: Right
    TextBox TextBox txtSaturday1 0.00 TextAlign: Right
    TextBox TextBox txtSunday1 0.00 TextAlign: Right
    Label Label   Second Week:  
    TextBox TextBox txtMonday2 0.00 TextAlign: Right
    TextBox TextBox txtTuesday2 0.00 TextAlign: Right
    TextBox TextBox txtWednesday2 0.00 TextAlign: Right
    TextBox TextBox txtThursday2 0.00 TextAlign: Right
    TextBox TextBox txtFriday2 0.00 TextAlign: Right
    TextBox TextBox txtSaturday2 0.00 TextAlign: Right
    TextBox TextBox txtSunday2 0.00 TextAlign: Right
    GroupBox GroupBox   Payroll Processing  
    Label Label   Hours  
    Label Label   Amount  
    Button Button btnCalculate Calculate  
    Label Label   Regular  
    TextBox TextBox txtRegularTime 0.00 TextAlign: Right
    TextBox TextBox txtRegularAmount 0.00 TextAlign: Right
    Label Label   Net Pay:  
    TextBox TextBox txtNetPay 0.00 TextAlign: Right
    Label Label   Overtime  
    TextBox TextBox txtOvertime 0.00 TextAlign: Right
    TextBox TextBox txtOvertimeAmount 0.00 TextAlign: Right
    Button Button btnClose    
  5. On the form, double-click the Calculate button and implement its event as follows:
     
    private void btnCalculate_Click(object sender, EventArgs e)
    {
            double monday1 = 0.00, tuesday1 = 0.00, wednesday1 = 0.00,
                        thursday1 = 0.00, friday1 = 0.00, saturday1 = 0.00,
                        sunday1 = 0.00, monday2 = 0.00, tuesday2 = 0.00,
                        wednesday2 = 0.00, thursday2 = 0.00,
             friday2 = 0.00, saturday2 = 0.00, sunday2 = 0.00;
             double totalHoursWeek1, totalHoursWeek2;
    
             double regHours1 = 0.00, regHours2 = 0.00,
                         ovtHours1 = 0.00, ovtHours2 = 0.00;
             double regAmount1 = 0.00, regAmount2 = 0.00,
                         ovtAmount1 = 0.00, ovtAmount2 = 0.00;
             double regularHours, overtimeHours;
             double regularAmount, overtimeAmount, totalEarnings;
    
             double hourlySalary = 0.00;
    
             // Retrieve the hourly salary
             try
             {
                    hourlySalary = double.Parse(txtHourlySalary.Text);
             }
             catch (FormatException)
             {
                    MessageBox.Show(
                        "The value you typed for the salary is invalid \n" +
                        "Please try again");
                    txtHourlySalary.Focus();
             }
    
             // Retrieve the value of each day worked
             try
             {
                    monday1 = double.Parse(txtMonday1.Text);
             }
             catch (FormatException)
             {
                    MessageBox.Show("You typed an invalid value\n" +
                             "Please try again");
                    txtMonday1.Focus();
             }
             try
             {
                    tuesday1 = double.Parse(txtTuesday1.Text);
             }
             catch (FormatException)
             {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    this.txtTuesday1.Focus();
             }
             try
             {
                    wednesday1 = double.Parse(txtWednesday1.Text);
             }
             catch (FormatException)
             {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    txtWednesday1.Focus();
             }
             try
             {
                    thursday1 = double.Parse(txtThursday1.Text);
             }
             catch (FormatException)
             {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    txtThursday1.Focus();
             }
    
             try
             {
                    friday1 = double.Parse(txtFriday1.Text);
             }
             catch (FormatException)
             {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    txtFriday1.Focus();
             }
    
             try
             {
                    saturday1 = double.Parse(txtSaturday1.Text);
             }
             catch (FormatException)
             {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    txtSaturday1.Focus();
             }
    
             try
             {
                    sunday1 = double.Parse(txtSunday1.Text);
             }
             catch (FormatException)
             {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    txtSunday1.Focus();
            }
            try
            {
                    monday2 = double.Parse(txtMonday2.Text);
            }
            catch (FormatException)
            {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    this.txtMonday2.Focus();
            }
            try
            {
                    tuesday2 = double.Parse(txtTuesday2.Text);
            }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    this.txtTuesday2.Focus();
                }
                try
                {
                    wednesday2 = double.Parse(txtWednesday2.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    this.txtWednesday2.Focus();
                }
                try
                {
                    thursday2 = double.Parse(txtThursday2.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    txtThursday2.Focus();
                }
                try
                {
                    friday2 = double.Parse(txtFriday2.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    txtFriday2.Focus();
                }
                try
                {
                    saturday2 = double.Parse(txtSaturday2.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    txtSaturday2.Focus();
                }
                try
                {
                    sunday2 = double.Parse(txtSunday2.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value\n" +
                        "Please try again");
                    txtSunday2.Focus();
                }
    
                // Calculate the total number of hours for each week
                totalHoursWeek1 = monday1 + tuesday1 + wednesday1 +
                      thursday1 + friday1 + saturday1 + sunday1;
                totalHoursWeek2 = monday2 + tuesday2 + wednesday2 +
                    thursday2 + friday2 + saturday2 + sunday2;
    
                // The overtime is paid time and half
                double ovtSalary = hourlySalary * 1.5;
    
                // If the employee worked under 40 hours, there is no overtime
                if (totalHoursWeek1 < 40)
                {
                    regHours1 = totalHoursWeek1;
                    regAmount1 = hourlySalary * regHours1;
                    ovtHours1 = 0.00;
                    ovtAmount1 = 0.00;
                } // If the employee worked over 40 hours, calculate the overtime
                else if (totalHoursWeek1 >= 40)
                {
                    regHours1 = 40;
                    regAmount1 = hourlySalary * 40;
                    ovtHours1 = totalHoursWeek1 - 40;
                    ovtAmount1 = ovtHours1 * ovtSalary;
                }
    
                if (totalHoursWeek2 < 40)
                {
                    regHours2 = totalHoursWeek2;
                    regAmount2 = hourlySalary * regHours2;
                    ovtHours2 = 0.00;
                    ovtAmount2 = 0.00;
                }
                else if (totalHoursWeek2 >= 40)
                {
                    regHours2 = 40;
                    regAmount2 = hourlySalary * 40;
                    ovtHours2 = totalHoursWeek2 - 40;
                    ovtAmount2 = ovtHours2 * ovtSalary;
                }
    
                regularHours = regHours1 + regHours2;
                overtimeHours = ovtHours1 + ovtHours2;
                regularAmount = regAmount1 + regAmount2;
                overtimeAmount = ovtAmount1 + ovtAmount2;
                totalEarnings = regularAmount + overtimeAmount;
    
                txtRegularTime.Text = regularHours.ToString("F");
                txtOvertime.Text = overtimeHours.ToString("F");
                txtRegularAmount.Text = regularAmount.ToString("F");
                txtOvertimeAmount.Text = overtimeAmount.ToString("F");
    
                txtNetPay.Text = totalEarnings.ToString("F");
    }
  6. Save the form
 

Home Copyright © 2007 FunctionX, Inc. Next