ComboBox控件:显示一个可编辑的文本框,其中包含一个允许值下拉的列表。
属性:
Items:组合框中的项。
DropDownStyle:控制组合框的外观和功能。
事件:
SelectedIndexChanged:SelectedIndex属性值更改时发生。

public partial class Form1 : Form{public Form1(){InitializeComponent();}int i = 0;//点击butAdd给comboBox2增加下拉选项private void butAdd_Click(object sender, EventArgs e){comboBox2.Items.Add(i);i++;}private void butClear_Click(object sender, EventArgs e){comboBox2.Items.Clear();}}
日期选择器:
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--){cboYear.Items.Add(i + "年");}}/// <summary>/// 当年份发生改变的时候,加载月份/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void cboYear_SelectedIndexChanged(object sender, EventArgs e){//每次cboYear.ItemselectedIndex属性值更改时都会给cboMonth添加12个月//所以应该清除cboMonth.Items.Clear();for (int i = 1; i <= 12; i++){cboMonth.Items.Add(i + "月");}}/// <summary>/// 当月份发生改变的时候,加载日期/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void cboMonth_SelectedIndexChanged(object sender, EventArgs e){int day = 0;//每次cboMonth.ItemselectedIndex属性值更改时都会给cboDay添加日期//所以应该清除cboDay.Items.Clear();//利用.SelectedItem属性ToString方法获得cboMonth,cboYear的选定的文本 再利字符串Split方法获得数字or直接中substring方法截掉最后一位//string s = cboMonth.SelectedText;//获取不了//MessageBox.Show(s);string stryear = cboYear.SelectedItem.ToString();string strmonth = cboMonth.SelectedItem.ToString();//获得年月份的数字://使用substring方法截掉最后一位string strMonth = strmonth.Substring(0, strmonth.Length-1);string strYear = stryear.Substring(0, stryear.Length - 1);//使用Split方法获得数字//string[] strArray = strmonth.Split(new char[] { '月' }, StringSplitOptions.RemoveEmptyEntries);//string strMonth = strArray[0];//string strMonth = strmonth.Split(new char[] { '月' }, StringSplitOptions.RemoveEmptyEntries)[0];//string strYear = stryear.Split(new char[] { '年' }, StringSplitOptions.RemoveEmptyEntries)[0];//将年月份字符串转换成int类型int year = Convert.ToInt32(strYear);int month = Convert.ToInt32(strMonth);//利用switch获得dayswitch (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;}//给cboDay下拉框赋值for (int i = 1; i <= day; i++){cboDay.Items.Add(i + "日");}}}
