欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

运行效果如下:

 

实例代码如下:

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>
        /// 本次操作所选日期列表
        /// </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);//当前月上一月的总天数
            DateTime oneDay = new DateTime(year, month, 1);//当前月的第一天
            int count = GetDays(oneDay.ToString("dddd"));
            //初始化上一月日期信息
            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;
                }
            }
            //当前月日期初始化
            int days = DateTime.DaysInMonth(year, 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;
                }
            }
            //下一月日期初始化
            DateTime downMonth = new DateTime(year, month, 1);
            if (month != 12) downMonth = new DateTime(year, month + 1, 1);//当前月的第一天
            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 "星期一":
                    return 1;
                case "星期二":
                    return 2;
                case "星期三":
                    return 3;
                case "星期四":
                    return 4;
                case "星期五":
                    return 5;
                case "星期六":
                    return 6;
                case "星期天":
                case "星期日":
                    return 7;
            }
            return 0;
        }

        private void InitYearAndMonth()
        {
            List<int> years = new List<int>();
            for (int i = -5; i <= 5; i++)//前置5年,后置5年 共11年
            {
                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());
        }


    }
}

  

posted on 2018-05-02 16:37  sunwugang  阅读(1254)  评论(0编辑  收藏  举报