【发布时间】:2018-02-14 08:27:52
【问题描述】:
我正在为组合框中的每个项目添加一个“索引”对象
foreach(索引中的变量索引) { UniqueIndexComboBox.Items.Add(index); }
当用户从下拉列表中选择一个索引项时,会触发以下事件。我不确定有什么区别。
private void UniqueIndexComboBox_SelectedValueChanged(object sender, EventArgs e) private void UniqueIndexComboBox_SelectedIndexChanged(object sender, EventArgs e)
当我整合以下属性时,SelectedValue 始终为 null,但我仍然可以通过使用 SelectedIndex 值作为项目列表的索引来访问选定的索引值。
使用 WinForm ComboBox,为什么会选择 ? UniqueIndexComboBox.Items[UniqueIndexComboBox.SelectedIndex] == null 错误的 ? UniqueIndexComboBox.SelectedValue == null 真的
为什么 SelectedValue 选项也不起作用? DropDownStyle 属性的值是否相关?
【问题讨论】:
-
SelectedValue需要ValueMember属性并且使用循环填充ComboBox不会设置ValueMember,您需要将索引作为数据源。 -
正如 Berkay 所说,
SelectedIndex属性是项目列表中当前选定项目的从零开始的索引,如果没有选择项目,则为 -1。不需要为值添加(数据库启发?)索引。有关详细信息,请参阅 MSDN 文档。如果SelectedIndex>= 0,则UniqueIndexComboBox.Items[UniqueIndexComboBox.SelectedIndex]始终为非空值,因为选择了一个项目。如果 'ComboBox' 是数据绑定的,则使用SelectedValue。
标签: winforms