public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { } /// <summary> /// 当年份发生改变的时候 加载月份 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { //添加之前应该清空之前的内容 cboMonths.Items.Clear(); for (int i = 1; i <= 12; i++) { cboMonths.Items.Add(i + "月"); } } private void Form1_Load(object sender, EventArgs e) { //程序加载时,将年份加到下拉框中 //获得当前年份 int year = DateTime.Now.Year; for (int i = year; i >= 1949; i--) { cboYears.Items.Add(i + "年"); } } /// <summary> /// 当选中月份时 加载日期 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cboMonths_SelectedIndexChanged(object sender, EventArgs e) { cboDays.Items.Clear(); int day = 0; //获得月份 string strMonth = cboMonths.SelectedItem.ToString().Split(new char[] { ‘月‘ })[0]; string strYear = cboYears.SelectedItem.ToString().Split(new char[] { ‘年‘ })[0]; int year = Convert.ToInt32(strYear); int month = Convert.ToInt32(strMonth); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day = 31; break; case 2: if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { day = 29; } else { day = 28; } break; default: day = 30; break; } for (int i = 1; i <= day; i++) { cboDays.Items.Add(i + "日"); } } }
时间: 2024-10-13 18:10:54