using System; using System.Windows.Forms; namespace DateDemo { public partial class Form1 : Form { private int _currentYearIndex; private int _currentMonthIndex; private const int StartYear = 1949; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DateTime today = DateTime.Today; for (var i = today.Year; i >=StartYear; i--) { cbYear.Items.Add(i); } for (var i = 1; i < 13; i++) { cbMonth.Items.Add(i); } for (var i = 1; i < GetDays(today.Year, today.Month); i++) { cbDay.Items.Add(i); } cbYear.SelectedIndex = 0; cbMonth.SelectedIndex = today.Month - 1; cbDay.SelectedIndex = today.Day - 1; } private int GetDays(int year, int month) { var days = 0; switch (month) { case 4: case 6: case 9: case 11: days = 30; break; case 2: days = IsLeapYear(year) ? 29 : 28; break; default: days = 31; break; } return days; } public static bool IsLeapYear(int year) { return year % 400 == 0 || year % 4 == 0 && year % 100 != 0; } private void cbYear_SelectedIndexChanged(object sender, EventArgs e) { _currentYearIndex = cbYear.SelectedIndex; int y = DateTime.Today.Year- cbYear.SelectedIndex; int m = _currentMonthIndex + 1; // MessageBox.Show(y+" "+m); SettingCbDay(y, m); cbDay.SelectedIndex = 0; } private void SettingCbDay(int y, int m) { cbDay.Items.Clear(); for (var i = 1; i <= GetDays(y, m); i++) { cbDay.Items.Add(i); } } private void cbMonth_SelectedIndexChanged(object sender, EventArgs e) { _currentMonthIndex = cbMonth.SelectedIndex; int y = DateTime.Today.Year - cbYear.SelectedIndex; int m = cbMonth.SelectedIndex + 1; // MessageBox.Show(y + " " + m); SettingCbDay(y, m); cbDay.SelectedIndex = 0; } } }