自制WindowsForms日历★新建WindowsForms应用程序。建立一个窗体DateForm。加入几个控件:ComboBox:cboYear(年)ComboBox:cboMonth(月)DataGridView:dgvDate加入7个列分别代表星期日、一、二、三、四、五、六RichTextBox:rtbInfo查看当前选中日期信息如下图:★需要处理的内容:一、在窗体的Load事件中初始化年、月、以及加载当前的年月日(窗体加载事件Load)二、当年改变时,dgv表格日期的改变(选择的索引改变事件SelectedIndexChanged)三、对月改变时,dgv表格日期的改变(选择的索引改变事件SelectedIndexChanged)四、选中一个日期时,显示信息(表格的单元格获得焦点事件CellEnter)★程序的主要代码如下(窗体生成的代码略):usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceYlqForm{publicpartialclassDateForm:Form{//是否激活下拉框选择内容的改变事件全部加载基本内容后再激活boolenabledSelectedEvent=false;publicDateForm(){InitializeComponent();}///summary///初始化数据设置年的范围为~2099年之间////summaryprivatevoidInitialData(){//年DataTabledt=newDataTable();dt.Columns.Add(ID,typeof(int));dt.Columns.Add(Name,typeof(string));for(inti=1900;i2100;i++){dt.Rows.Add(i,i+年);}cboYear.DataSource=dt;cboYear.DisplayMember=Name;cboYear.ValueMember=ID;//月DataTabledtMonth=dt.Clone();dtMonth.Rows.Add(1,一月);dtMonth.Rows.Add(2,二月);dtMonth.Rows.Add(3,三月);dtMonth.Rows.Add(4,四月);dtMonth.Rows.Add(5,五月);dtMonth.Rows.Add(6,六月);dtMonth.Rows.Add(7,七月);dtMonth.Rows.Add(8,八月);dtMonth.Rows.Add(9,九月);dtMonth.Rows.Add(10,十月);dtMonth.Rows.Add(11,十一月);dtMonth.Rows.Add(12,十二月);cboMonth.DataSource=dtMonth;cboMonth.DisplayMember=Name;cboMonth.ValueMember=ID;}///summary///窗体加载事件////summary///paramname=sender/param///paramname=e/paramprivatevoidDateForm_Load(objectsender,EventArgse){//只能选择一个单元格、列不可排序dgvDate.MultiSelect=false;for(inti=0;i7;i++){dgvDate.Columns[i].SortMode=DataGridViewColumnSortMode.NotSortable;}InitialData();//默认为当前年月DateTimecurrentDate=DateTime.Now;for(inti=0;icboYear.Items.Count;i++){DataRowViewdrv=cboYear.Items[i]asDataRowView;if(Convert.ToInt32(drv.Row[ID])==currentDate.Year){cboYear.SelectedIndex=i;break;}}for(inti=0;icboMonth.Items.Count;i++){DataRowViewdrv=cboMonth.Items[i]asDataRowView;if(Convert.ToInt32(drv.Row[ID])==currentDate.Month){cboMonth.SelectedIndex=i;break;}}DisplayDate(currentDate.Year,currentDate.Month);for(inti=0;idgvDate.Rows.Count;i++){boolisBreak=false;for(intj=0;j7;j++){if(Convert.ToString(dgvDate[j,i].Value)==currentDate.Day.ToString()){isBreak=true;dgvDate[j,i].Selected=true;break;}}if(isBreak){break;}}//D2:格式化为位数字不足位则前面补rtbInfo.Text=string.Format(当前日期:{0}年{1:D2}月{2:D2}日{3},currentDate.Year,currentDate.Month,currentDate.Day,GetWeekDay(currentDate.DayOfWeek));enabledSelectedEvent=true;////{{将输出一个左大括号}}将输出一个右大括号//MessageBox.Show(string.Format({{{0}---{1}}},abc,123));}///summary///显示某年某月的日历////summary///paramname=year/param///paramname=month/paramprivatevoidDisplayDate(intyear,intmonth){dgvDate.Rows.Clear();DateTimefirstDay=newDateTime(year,month,1);intcolumnIndex=(int)firstDay.DayOfWeek;intmaxDay=GetMaxDay(year,month);//某月的第一天的星期数对应列的索引即当月天数需要额外增加columnIndex//当月总天数设定为:columnIndex+maxDay。总分页数:(x+(n-1))/n其中n=7//表格共有行数:(columnIndex+maxDay+6)/7取整intday=1;for(inti=0;i(columnIndex+maxDay+6)/7;i++){intindex=dgvDate.Rows.Add();for(intj=0;j7;j++){if(i==0)//第一行从对应的星期开始填写{if(j=columnIndex){dgvDate[j,index].Value=day;day++;}}else{dgvDate[j,index].Value=day;day++;if(daymaxDay){break;}}}}}///summary///获得某年某月有多少天////summary///paramname=year/param///paramname=month/param///returns/returnsprivateintGetMaxDay(intyear,intmonth){boolisLeapYear=DateTime.IsLeapYear(year);switch(month){case1:case3:case5:case7:case8:case10:case12:return31;case4:case6:case9:case11:return30;case2:returnisLeapYear?29:28;default:return0;}}///summary///获得星期的中文描述////summary///paramname=week/param///returns/returnsprivatestringGetWeekDay(DayOfWeekweek){switch(week){caseDayOfWeek.Sunday:return星期日;caseDayOfWeek.Monday:return星期一;caseDayOfWeek.Tuesday:return星期二;caseDayOfWeek.Wednesday:return星期三;caseDayOfWeek.Thursday:return星期四;caseDayOfWeek.Friday:return星期五;caseDayOfWeek.Saturday:return星期六;default:return;}}///summary///年、月改变事件////summary///paramname=sender/param///paramname=e/paramprivatevoidcboYear_SelectedIndexChanged(objectsender,EventArgse){if(enabledSelectedEvent){DisplayDate(Convert.ToInt32(cboYear.SelectedValue),Convert.ToInt32(cboMonth.SelectedValue));}}///summary///当前单元格更改或者该单元格接收到输入焦点时发生提示当前选中日期////summaryprivatevoiddgvDate_CellEnter(objectsender,DataGridViewCellEventArgse){if(dgvDate.SelectedCells.Count1)return;objectday=dgvDate.SelectedCells[0].Value;if(Convert.ToInt32(day)0){DateTimecurrentDate=newDateTime(Convert.ToInt32(cboYear.SelectedValue),Convert.ToInt32(cboMonth.SelectedValue),Convert.ToInt32(day));rtbInfo.Text=string.Format(当前日期:{0}年{1:D2}月{2:D2}日{3},currentDate.Year,currentDate.Month,currentDate.Day,GetWeekDay(currentDate.DayOfWeek));}else{rtbInfo.Text=;}}}}★程序执行结果:Load时:年、月改变后: