标签:initial change break namespace orm 获得 win 添加 date
ComboBox重要属性:【DropDownStyle】 获取或设置指定组合框样式的值。
下面仍然通过一个小程序来回顾:
代码如下:
using System; using System.Windows.Forms; namespace 日期选择器 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //在程序加载的时候,添加年份 private void Form1_Load(object sender, EventArgs e) { //获得当前的年份 int year = DateTime.Now.Year; //添加年份 for (int i = year; i >= 1949; i--) { cmbYear.Items.Add(i + "年"); } } //当年份发生改变的时候加载月份 private void cmbYear_SelectedIndexChanged(object sender, EventArgs e) { //添加之前,应该先清理 cmbMonth.Items.Clear(); //添加 for (int i = 1; i <= 12; i++) { cmbMonth.Items.Add(i + "月"); } } //当月份发生改变的时候,加载日子 private void cmbMonth_SelectedIndexChanged(object sender, EventArgs e) { cmbDay.Items.Clear(); //获得年份、月份 string strYear = cmbYear.SelectedItem.ToString().Split(new char[] { ‘年‘ }, StringSplitOptions.RemoveEmptyEntries)[0]; string strMonth = cmbMonth.SelectedItem.ToString().Split(new char[] { ‘月‘ }, StringSplitOptions.RemoveEmptyEntries)[0]; //转为整型 int year = Convert.ToInt32(strYear); int month = Convert.ToInt32(strMonth); //声明变量表示天数 int days = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 2: if (DateTime.IsLeapYear(year)) //判断闰年 { days = 29; } else { days = 28; } break; default: days = 30; break; } for (int i = 1; i <= days; i++) { cmbDay.Items.Add(i + "日"); } } } }
源代码链接:http://pan.baidu.com/s/1pL9eTsf 密码:wvts
标签:initial change break namespace orm 获得 win 添加 date
原文地址:http://www.cnblogs.com/lolitagis/p/7479620.html