已选择项目更改组合框
问题描述:
只有在某些情况下,是否有任何方法可以防止更改ComboBox中的选定项目?我想允许更新ComboBox中所选项目的displayValue。但我不希望用户在更新时更改所选项目。这是一个Windows应用程序。已选择项目更改组合框
答
你的类中:
private int _selectedIndex = 0;
内,您的形式加载方法:
comboBox1.Enter += new EventHandler(comboBox1_Enter);
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
那么剩下的代码:
protected void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
if (true) { // Add your validation or certain condition here.
(sender as ComboBox).SelectedIndex = _selectedIndex;
}
}
protected void comboBox1_Enter(object sender, EventArgs e) {
_selectedIndex = (sender as ComboBox).SelectedIndex;
}
感谢您的回复。但我想阻止打开物品清单。该解决方案对用户来说不会造成混淆。 – CharithJ 2011-05-03 01:55:44