2-13  monthCalendar日历控件

u本节学习目标:
n了解monthCalendar控件的属性
n学习monthCalendar控件属性的动态控制

    2-13-1  简介

Windows 窗体 MonthCalendar 控件为用户查看和设置日期信息提供了一个直观的图形界面。该控件以网格形式显示日历,网格包含月份的编号日期,这些日期排列在周一到周日下的七个列中,并且突出显示选定的日期范围。可以单击月份标题任何一侧的箭头按钮来选择不同的月份。与类似的 DateTimePicker 控件不同,您可以使用该控件选择多个日期,但其选择范围仅限一周(按住shift键用鼠标点击范围)。monthCalendar控件通常用于选择日期,典型的日历控件如图2-42所示:
2-42  monthCalendar日历控件样式

    2-13-2  monthCalendar控件的属性

monthCalendar控件主要的属性如表2-13所示:
属性
说明
Backcolor
月份中显示背景色。
SelectionRange
在月历中显示的起始时间范围,Begin为开始,end为截至。
Minmum
最小值,默认0.
Showtody,
showtodaycircle,
Showweeknumbers
是否显示今天日期
是否在今天日期上加红圈
是否左侧显示周数(1-52周)
Titlebackcolor
TitleForcolor
Trailingcolor
日历标题背景色。
日历标题前景色
上下月颜色
2-13 monthCalendar控件的属性

    2-13-3  monthCalendar控件实践操作

1. 案例学习:monthCalendar控件属性动态控制

1)从工具箱之中拖放一个monthCalendar控件,三个ComBoBox控件和若干Label标签,如图2-43所示进行布局。
2-43  monthCalendar控件实验布局
2)具体功能的实现见下面的编码部分:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsApplication2
{
    public partial class Form007 : Form
    {
        public Form007()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 初始化时候,首先设置label标签的内容
        /// </summary>
        private void Form007_Load(object sender, EventArgs e)
        {
            label1.Text = "今天是:" + monthCalendar1.TodayDate.ToString();
            label2.Text = "";
            label3.Text = "";
            label7.Text = "";
            label8.Text = "";
            label9.Text = "";
        }
        /// <summary>
        /// 一旦monthCalendar控件的时间发生变化,则引发label标签的内容改变。
        /// </summary>
        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            label2.Text = "起始日期:" + monthCalendar1.SelectionStart.ToString();
            //学习monthCalendar属性SelectionStart,表示获取起始日期
            label3.Text = "结束日期:" + monthCalendar1.SelectionEnd.ToString();
            //学习monthCalendar属性SelectionEnd,表示获取结束日期
            label7.Text = "加3月日期:" + monthCalendar1.SelectionStart.AddMonths(3).ToString();
            //学习monthCalendar方法AddMonths,表示增加起始日期的月份
            label8.Text = "加3天日期:" + monthCalendar1.SelectionStart.AddDays(3).ToString();
            //学习monthCalendar方法AddDays,表示增加起始日期的天数
            label9.Text = "加3年日期:" + monthCalendar1.SelectionStart.AddYears(3).ToString();
            //学习monthCalendar方法AddYears,表示增加起始日期的年份
        }
        /// <summary>
        /// 当改变背景色时候出发代码,本案例熟悉monthCalendar控件的TitleBackColor属性
        /// </summary>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex >= 0)
            {
                int i = comboBox1.SelectedIndex;
                switch (i)
                {
                    case 0:
                        monthCalendar1.TitleBackColor = System.Drawing.Color.Red;
                        break;
                    case 1:
                        monthCalendar1.TitleBackColor = System.Drawing.Color.Yellow;
                        break;
                    case 2:
                        monthCalendar1.TitleBackColor = System.Drawing.Color.Blue;
                        break;
                    case 3:
                        monthCalendar1.TitleBackColor = System.Drawing.Color.Green;
                        break;
                }
            }
        }
        /// <summary>
        /// 当改变标题色时候出发代码,本案例熟悉monthCalendar控件的TrailingForeColor属性
        /// </summary>
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex >= 0)
            {
                int i = comboBox2.SelectedIndex;
                switch (i)
                {
                    case 0:
                        monthCalendar1.TrailingForeColor = System.Drawing.Color.Red;
                        break;
                    case 1:
                        monthCalendar1.TrailingForeColor = System.Drawing.Color.Yellow;
                        break;
                    case 2:
                        monthCalendar1.TrailingForeColor = System.Drawing.Color.Blue;
                        break;
                    case 3:
                        monthCalendar1.TrailingForeColor = System.Drawing.Color.Green;
                        break;
                }
            }
        }
        /// <summary>
        /// 当改变前景色时候出发代码,本案例熟悉monthCalendar控件的TitleForeColo属性
        /// </summary>
        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox3.SelectedIndex >= 0)
            {
                int i = comboBox3.SelectedIndex;
                switch (i)
                {
                    case 0:
                        monthCalendar1.TitleForeColor = System.Drawing.Color.Red;
                        break;
                    case 1:
                        monthCalendar1.TitleForeColor = System.Drawing.Color.Yellow;
                        break;
                    case 2:
                        monthCalendar1.TitleForeColor = System.Drawing.Color.Blue;
                        break;
                    case 3:
                        monthCalendar1.TitleForeColor = System.Drawing.Color.Green;
                        break;
                }
            }
        }
    }
}