Combobox的使用,日期选择器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; namespace Combobox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { int year = DateTime.Now.Year; for (int i = 0; i <= 20; i++ ) { cboYear.Items.Add((year - i)+"年"); } } private void button1_Click(object sender, EventArgs e) { cboYear.Items.Remove("2"); } private void button2_Click(object sender, EventArgs e) { cboYear.Items.Clear(); } private void cboYear_SelectedIndexChanged(object sender, EventArgs e) { if (cboMonth.Items.Count != 0) return; for(int i = 1; i <= 12; i++) { cboMonth.Items.Add(i + "月"); } } private void cboMonth_SelectedIndexChanged(object sender, EventArgs e) { cboDay.Items.Clear(); int day = 0; //cboMonth.SelectedItem.ToString() 返回1月,要去除月 //Split(new char[] {'月'},StringSplitOptions.RemoveEmptyEntries) 返回数组 string strMonth = cboMonth.SelectedItem.ToString().Split(new char[] {'月'},StringSplitOptions.RemoveEmptyEntries)[0]; string strYear = cboYear.SelectedItem.ToString().Split(new char[] {'年'},StringSplitOptions.RemoveEmptyEntries)[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++) { cboDay.Items.Add(i + "日"); } } } }
重要属性DropDownStyle