C#学习记录

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();
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值