C#中MonthCalendar控件应用实例
MonthCalendar控件
功能
MonthCalendar控件为用户查看和设置日期信息提供了一个直观的图形界面。该控件以网格形式显示日历,网格包含月份的编号日期,这些日期排列在周一到周日下的7个列中,并且突出显示选定的日期范围。可以单击月份标题任何一侧的箭头按钮来选择不同的月份,与DateTimePicker控件不同的是,开发人员可以使用该控件选择多个日期。图1为MonthCalendar控件。

图1 MonthCalendar x控件
2.属性
MonthCalendar控件常用属性及说明如表1所示。

表1 MonthCalendar控件常用属性及说明
下面对比较重要的属性进行详细介绍。
(1)ShowToday属性。此属性把今天的日期显示在控件底部。
语法:
public bool ShowToday { get; set; }
属性值:如果显示的是今天的日期,为True;否则为False。默认值为True。
(2)howTodayCircle属性。此属性用来将今天的日期显示为圆形。
语法:
public bool ShowTodayCircle { get; set; }
属性值:如果在今天的日期上加圆圈,则为True;否则为False。默认值为True。
示例
ShowTodayCircle属性的使用
本示例中,当程序运行时,单击【ShowTodayCircle属性】按钮,在该按钮的Click事件中设置MonthCalendar控件的ShowTodayCircle属性,以便在当前的日期上加圆圈。
程序主要代码如下:
private void button2_Click(object sender, EventArgs e)
{
this.monthCalendar1.ShowTodayCircle = True;
}
3.方法
AddBoldedDate方法用于在月历中添加以粗体显示的日期。
语法:
public void AddBoldedDate (DateTime date)
date:要以粗体显示的日期。
完整程序代码如下:
★★★★★主程序文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _8_14
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMonthCalendar());
}
}
}
★★★★★frmMonthCalendar窗体设计文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _8_14
{
public partial class frmMonthCalendar : Form
{
public frmMonthCalendar()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.monthCalendar1.ShowTodayCircle = true;
}
private void button3_Click(object sender, EventArgs e)
{
}
private void frmMonthCalendar_Load(object sender, EventArgs e)
{
}
}
}
★★★★★frmMonthCalendar窗体代码文件完整程序代码★★★★★
namespace _8_14
{
partial class frmMonthCalendar
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.monthCalendar1 = new System.Windows.Forms.MonthCalendar();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// monthCalendar1
//
this.monthCalendar1.Location = new System.Drawing.Point(34, 5);
this.monthCalendar1.Name = "monthCalendar1";