一、conbox最最简单的用法
效果是这样的:
代码:
private void Form1_Load(object sender, EventArgs e){comboBox1.Items.Add("平方差");comboBox1.Items.Add("归一化的平方差");comboBox1.Items.Add("相关性匹配");comboBox1.Items.Add("归一化的相关性匹配");comboBox1.Items.Add("相关性系数匹配");comboBox1.Items.Add("归一化的相关性系数匹配");}public int TemplateMachingMethod = 0; private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){switch (comboBox1.SelectedItem.ToString()) //获取选择的内容{case "平方差": TemplateMachingMethod = 1; break;case "归一化的平方差": TemplateMachingMethod = 2; break;case "相关性匹配": TemplateMachingMethod = 3; break;case "归一化的相关性匹配": TemplateMachingMethod = 4; break;case "相关性系数匹配": TemplateMachingMethod = 5; break;case "归一化的相关性系数匹配": TemplateMachingMethod = 6; break;}}
二、再高级一点就这样写
//POJO:class ComboBoxItem{string _text;string _value;public string Text{get { return _text; }set { _text = value; }}public string Value{get { return _value; }set { _value = value; }}public override String ToString() {return this.Value;}}
//Init:
//注:cboURL是combox的控件名、SurferWithProxy是窗口名private void SurferWithProxy_Load(object sender, EventArgs e){intiComboBox();}private void intiComboBox() { ComboBoxItem cbi = new ComboBoxItem();cbi = new ComboBoxItem();cbi.Text = "test1";cbi.Value = "Value1";this.cboURL.Items.Add(cbi);cbi = new ComboBoxItem();cbi.Text = "test2";cbi.Value = "Value2";this.cboURL.Items.Add(cbi);cbi = new ComboBoxItem();cbi.Text = "test3";cbi.Value = "Value3";this.cboURL.Items.Add(cbi);cbi = new ComboBoxItem();cbi.Text = "test4";cbi.Value = "Value4";this.cboURL.Items.Add(cbi);this.cboURL.DisplayMember = "Text";this.cboURL.ValueMember = "Value";}
补充:其他操作和常用属性
1)Text属性:获取当前显示的文本
2)SelectedText属性:获得当前选中的文本(控件获得光标且DropDown属性不为DropDownList)
注意:但应注意,所选内容会因用户交互而自动更改。如Button的Click事件中,SelectedIndexChanged 或 SelectedValueChanged 事件中,此属性会返回空字符串(参见MSCN:http://msdn.microsoft.com/zh-cn/partners/system.windows.forms.combobox.selectedtext(VS.90).aspx )
3)SelectedValue属性:当前显示项对应的Value值(仅在绑定数据源时,设置了ValueMember时才可以用)
4)SelectedItem属性:控件当前选中项
5)SelectedIndex属性:当前选中项的索引