运行效果如下:
实例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 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()); } } } |
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本