MonthCalendar控件(月历控件)
MonthCalendar控件(月历控件)提供了一个直观的图形界面,可以让用户查看和设置日期。 MonthCalendar控件中可以使用鼠标进行拖曳,用于选择一段连续的时间,此段连续的时间包括时间的起始和结束。
1.更Month改 Calendar控件的外观
MonthCalendar控件允许用多种方法自定义月历的外观。例如,可以设置配色方案并选择显示或隐藏周数和当前日期。
更改月历的配色方案
设置 TitleBackColor、 TitleForeColor和 TrailingForeColor等属性可以更改月历控件的配色方案。TitleBackColor属性用于设置日历标题区的背景色, TitleForeColor属性用于设置日历标题区的前景色, TrailingForeColor属性用于设置没有完全显示的日期的颜色。
显示周数
将 Show WeekNumbers属性设置为true实现在控件中显示周数。也可以用代码或在“属性”窗口中设置该属性。周数以单独的列出现在一周的第一天的左边。
如下例子
using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Test17
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
monthCalendar1.ShowWeekNumbers = true;
}
}
}
2.在 Month Calendar控件中显示多个月份
onthCalendar控件最多可同时显示12个月。默认情况下,控件只显示个月,但可以通过设置 CalendarDimensions属性指定显示多少个月以及它们在控件中的排列方式。当更改月历尺寸时,控件的大小也会随之改变,因此应确保窗体上有足够的空间供新尺寸使用。
例如
using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Test18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
monthCalendar1.CalendarDimensions = new Size(2, 2);
}
}
}
3.在 Month Calendar控件中选择日期范围
如果要在 Month Calendar控件中选择日期范围,必须设置 SelectionStart和 SelectionEnd属性。这两个属性分别用于设置日期的起始和结束。
例如
using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Test20
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = monthCalendar1.TodayDate.ToString();
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
textBox2.Text = monthCalendar1.SelectionStart.ToString();
textBox3.Text = monthCalendar1.SelectionEnd.ToString();
}
}
}