Combobox.Text Vs combobox.Selecteditem Vs combobox.selectValue?
这些中的每一个有什么区别? 我可以使用这些方法中的任何一种在标签中显示combobox的文本,还是有什么区别?
label1.Text = comboBox1.SelectedItem.ToString(); label2.Text = comboBox1.Text; label3.Text = comboBox1.SelectedValue.ToString();
我正在测试combobox的这些值,但我对它们如何工作感到困惑。 我想在标签中显示combobox的文本。 使用comboBox.Text它工作正常,但其余两个给出以下错误:
error message:Object reference not set to an instance of an object.
这是我的例子。
private void comboSelectChanged(object sender, SelectionChangedEventArgs e) { textBox1.Text = comboBox1.SelectedItem.ToString(); textBox2.Text = comboBox1.Text; textBox3.Text = comboBox1.SelectedIndex.ToString(); }
物品collections:
结果如下:
SelectedItem:获取或设置ComboBox中当前选定的项目。
基于ComboBox.SelectionChangeCommitted
Text:获取或设置与此控件关联的文本。 (重写Control.Text。)
设置文本值将改变combobox的当前值
SelectedValue:获取或设置ValueMember属性指定的成员属性的值。 (inheritance自ListControl。)
基于ListControl.SelectedValueChanged
此问题可能与ComboBox SelectedItem和SelectedValue重复。
来源msdn
进一步阅读dotnetperls 。