一、常用属性和用法

1、常用属性:

属性名

作用

DropDownStyle

获取或设置组合框的外观,如果值为Simple,同时显示文本框和列表框,并且文本框可以编辑;如果值为DropDown,则只显示文本框,通过鼠标或键盘的点击事件展开文本框,并且文本框可以编辑;如果值为DropDownList,显示效果与DropDown值一样,但文本框不可编辑。默认情况下为DropDown

Items

获取或设置组合框中的值

Text

获取或设置组合框中显示的文本

MaxDropDownItems

获取或设置组合框中最多显示的项数

Sorted

指定是否对组合框表中的项进行排序,如果值为True,则排序,如果值为False,则不排序。默认情况下为False

2、SelectedIndex:获取或设置指定当前选定项的索引。(设置新索引会引发SelectedIndexChanged 事件。)

3、SelectedItem:获取或设置 ComboBox 中当前选定的项。

//取得当前选择的Item的显示值

this.comboBox1.SelectedItem.ToString();

4、SelectedValue:获取或设置由 ValueMember 属性指定的成员属性的值。 类型:System.Object,包含由ValueMember 属性指定的数据源成员的值的对象。

当设置了DataSource 属性后,SelectedValue 属性值会默认为第一行,因此,如果不希望ComboBox自动选择第一行,还需在设置完DataSource 后自行将SelectedValue 设为“”。(根据SelectedValue的值类型设置,如果是字符就设置“”,如果是数值可设置为0)

5、SelectedText:获取或设置 ComboBox 的可编辑部分中选定的文本。如果 DropDownStyle 设置为 DropDownList,则返回值为空字符串 ("")。

6、设置默认值:

this.comboBox1.Text = "请选择内容";

//or

this.comboBox1.Items.Add("请选择内容");

this.comboBox1.SelectedIndex = 0;

7、数据绑定:

this.comboBox1.DataSource = dt;

this.comboBox1.DisplayMember = "dtColName";

this.comboBox1.ValueMember = "ID";

/*DisplayMember绑定的是需显示的字段,ValueMember绑定的是对应的值

一般DisplayMember是显示给客户看的, 而ValueMember 是绑定处理程序标识 给程序员看的*/

combobox控件显示的是DisplayMember 绑定的字段,也就是说comboBox1的text属性取的值是DisplayMember 绑定的字段。

8、获取ValueMember的值:

DataRowView drv = (DataRowView)comboBox1.SelectedItem;

ID= Convert.ToInt32(drv.Row["dtColName2"].ToString());

9、限制comboBox的显示个数:

this.comboBox1.IntegralHeight = false;

this.comboBox1.MaxDropDownItems = 5; //显示五个,超出则出现滚动条。

二、数据绑定方法

1、绑定List集合:

private void ListDataBinding()

{

Listlist = new List()

{

"test1","test2","test3"

};

this.comboBox1.DataSource = list;

}

2、绑定数组集合:

private void ArrayDataBinding()

{

string[] array = new string[] { "test1","test2","test3" };

this.comboBox1.DataSource = array;

}

3、绑定数组集合:

//创建一个实体类,用于存储数据

public class Data

{

public string ID { get; set; }

public string ShowValue { get; set; }

}

List datas = new List();

private void ArrayDataBinding()

{

Data data1 = new Data() { ID = "1", ShowValue = "test1" };

Data data2 = new Data() { ID = "2", ShowValue = "test2" };

Data data3 = new Data() { ID = "3", ShowValue = "test3" };

datas.Add(data1);

datas.Add(data2);

datas.Add(data3);

this.comboBox1.DataSource = datas;

this.comboBox1.DisplayMember = "ShowValue";

this.comboBox1.ValueMember = "ID";

}

//利用控件的SelectedIndexChanged事件选中的DisplayMember来查找对应的ValueMember。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{

this.textBox1.Text = comboBox1.SelectedValue.ToString();

}

结果如图:

4、绑定DataTable:

private void DataTableDataBinding()

{

DataTable dt = new DataTable();

DataColumn dc1 = new DataColumn("ID");

DataColumn dc2 = new DataColumn("ShowValue");

dt.Columns.Add(dc1);

dt.Columns.Add(dc2);

DataRow dr1 = dt.NewRow();

dr1["ID"] = "1";

dr1["ShowValue"] = "test1";

DataRow dr2 = dt.NewRow();

dr2["ID"] = "2";

dr2["ShowValue"] = "test2";

DataRow dr3 = dt.NewRow();

dr3["ID"] = "3";

dr3["ShowValue"] = "test3";

dt.Rows.Add(dr1);

dt.Rows.Add(dr2);

dt.Rows.Add(dr3);

this.comboBox1.DataSource = dt;

this.comboBox1.ValueMember = "ID";

this.comboBox1.DisplayMember = "ShowValue";

}

//利用控件的SelectedIndexChanged事件选中的DisplayMember来查找对应的ValueMember。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{

this.textBox1.Text = comboBox1.SelectedValue.ToString();

}

结果如图:

5、绑定枚举:

//定义一个颜色枚举

public enum ColorEnum

{

red,

blue,

skyblue

}

//使用Enum.GetNames()方法,将枚举元素的名称赋值给控件。

private void EnumDataBinding()

{

this.comboBox1.DataSource = Enum.GetNames(typeof(ColorEnum));

}

6、利用Items.Add方法添加元素:

private void ItemsDataBinding()

{

this.comboBox1.Items.Add("test1");

this.comboBox1.Items.Add("test2");

this.comboBox1.Items.Add("test3");

}

7、Items.AddRange:

private void AddRangeDataBinding()

{

object[] array = new object[] { "test1", "test2", "test3 "};

this.comboBox1.Items.AddRange(array);

}

Items.AddRange()方法里面的参数为Object类型的数组。

三、常用事件

1、SelectedIndexChanged事件:索引改变时即触发吗,可以通过编程触发。

2、SelectedValueChanged事件:值改变时触发,可以通过编程触发。

3、SelectionChangeCommitted事件:在设定datasource和displaymember和valuemember时,以及手动改变combobox索引和值时都不触发该事件, 只是在界面选择combobox不同值时触发,即只有通过界面点击修改combobox的值才会触发此事件。

关于在编程时通过设置DataSource, DisplayMember, ValueMember不同顺序均可触发SelectedIndexChanged事件和SelectedValueChanged事件的次数,但是不会触发SelectionChangeCommitted。

4、DropDown事件:显示 ComboBox 的下拉部分时发生。

5、DropDownClosed事件:当 ComboBox 的下拉部分不再可见时发生。