Date picker with checkbox

The operation effect is as follows:

 

The example code is as follows:

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

namespace Demo.UC
{
    public partial class ChbDater : UserControl
    {
        /// <summary>
        /// List of selected dates for this operation
        /// </summary>
        private List<string> dataList = new List<string>();
        public List<string> CurrentDataList
        {
            get { return this.dataList; }
            set
            {
                dataList = value;
            }
        }

        public ChbDater()
        {
            InitializeComponent();
        }

        private void ChbDater_Load(object sender, EventArgs e)
        {
            InitYearAndMonth();

            InitDaysControl(DateTime.Now.Year, DateTime.Now.Month);
        }

        private void InitDaysControl(int year, int month)
        {
            int uDays = 0;
            if (month != 1) uDays = DateTime.DaysInMonth(year, month - 1);//The total number of days in the previous month of the current month
            DateTime oneDay = new DateTime(year, month, 1);//The first day of the current month
            int count = GetDays(oneDay.ToString("dddd"));
            //Initialize the date information of the previous month
            int k = count;
            for (int i = count - 1; i > 0; i--)
            {
                k--;
                CheckBox chb = tlpcontent.Controls.Find("checkBox" + k, true)[0] as CheckBox;
                if (chb != null)
                {
                    if (k != 1) uDays--;
                    chb.Text = uDays.ToString();
                    chb.Visible = true;
                    chb.Enabled = false;
                }
            }
            // Initialize the current month and date
            int days = DateTime.DaysInMonth(year, month);//The total number of days in the current month
            for (int i = 0; i < days; i++)
            {
                CheckBox chb = tlpcontent.Controls.Find("checkBox" + (i + count), true)[0] as CheckBox;
                if (chb != null)
                {
                    //chb.Text = oneDay.AddDays(i).ToString("yyyy-MM-dd (dddd)");
                    chb.Text = oneDay.AddDays(i).ToString("dd");
                    chb.Enabled = true;
                    chb.Visible = true;
                }
            }
            //Initialize the next month's date
            DateTime downMonth = new DateTime(year, month, 1);
            if (month != 12) downMonth = new DateTime(year, month + 1, 1);//The first day of the current month
            int kk = 0;
            for (int i = (days + count); i <= 42; i++)
            {
                CheckBox chb = tlpcontent.Controls.Find("checkBox" + i, true)[0] as CheckBox;
                chb.Text = downMonth.AddDays(kk).ToString("dd");
                chb.Visible = true;
                chb.Enabled = false;
                kk ++;
            }
        }

        private int GetDays(string weekDay)
        {
            switch (weekDay)
            {
                case "Monday":
                    return 1;
                case "Tuesday":
                    return 2;
                case "Wednesday":
                    return 3;
                case "Thursday":
                    return 4;
                case "Friday":
                    return 5;
                case "Saturday":
                    return 6;
                case "Sunday":
                case "Sunday":
                    return 7;
            }
            return 0;
        }

        private void InitYearAndMonth()
        {
            List<int> years = new List<int>();
            for (int i = -5; i <= 5; i++)//The first 5 years, the last 5 years, a total of 11 years
            {
                if (i < 0) years.Add(DateTime.Now.Year + i);
                if (i >= 0) years.Add(DateTime.Now.Year + i);
            }

            this.cmbYear.DataSource = years;
            this.cmbYear.SelectedIndex = 5;
            this.cmbMonth.Text = DateTime.Now.Month.ToString();
        }

        private void cmbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.cmbYear.Text)) this.cmbYear.Text = DateTime.Now.Year.ToString();
            if (string.IsNullOrEmpty(this.cmbMonth.Text)) this.cmbMonth.Text = DateTime.Now.Month.ToString();
            InitDaysControl(int.Parse(this.cmbYear.Text), int.Parse(this.cmbMonth.Text));
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chb = sender as CheckBox;
            if (chb.Checked)
            {
                string month = this.cmbMonth.Text;
                if (int.Parse(month) < 10) month = "0" + month;
                CurrentDataList.Add(this.cmbYear.Text + "-" + month + "-" + chb.Text);
                chb.BackColor = Color.Wheat;
            }
            else
            {
                string month = this.cmbMonth.Text;
                if (int.Parse(month) < 10) month = "0" + month;
                CurrentDataList.Remove(this.cmbYear.Text + "-" + month + "-" + chb.Text);
                chb.BackColor = SystemColors.Control;
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            StringBuilder buf = new StringBuilder();
            foreach (string item in CurrentDataList)
            {
                buf.Append(item);
                buf.Append("\n");
            }
            MessageBox.Show(buf.ToString());
        }


    }
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325144641&siteId=291194637