C#日历控件(MonthCalendar)

日历控件(MonthCalendar)用于显示日期,通常是与文本框联用,将日期控件中选择的日期添加到文本框中。

【实例】

             使用日历控件实现入职日期的选择。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MonthCalendarForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //窗体加载事件
        private void Form1_Load(object sender, EventArgs e)
        {
            //隐藏日历控件
            monthCalendar1.Hide();
        }

        //“选择”按钮的单击事件
        private void button1_Click(object sender, EventArgs e)
        {
          //显示日历控件
            monthCalendar1.Show();
        }

        //日历控件的日期改变事件
        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
           //将选择的日期显示在文本框中
            textBox1.Text = monthCalendar1.SelectionStart.ToShortDateString();
            //隐藏日历控件
            monthCalendar1.Hide();
        }     
        
    }
}

                                    C#日历控件(MonthCalendar)_第1张图片           

                                     C#日历控件(MonthCalendar)_第2张图片

                                     C#日历控件(MonthCalendar)_第3张图片

你可能感兴趣的:(C#,WinForm)