已选择项目更改组合框

问题描述:

只有在某些情况下,是否有任何方法可以防止更改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; 
} 
+0

感谢您的回复。但我想阻止打开物品清单。该解决方案对用户来说不会造成混淆。 – CharithJ 2011-05-03 01:55:44

尝试设置将Enabled属性设置为false。 (或者一些第三方工具包像Telerik的有一个只读属性,一个ComboBox。)

+0

@大卫:我希望所有用户编辑所选项目的文本,但更改所选项目。 – CharithJ 2011-05-03 01:53:46

+0

对不起,我不明白。 – David 2011-05-03 01:58:49

+0

您可以输入和编辑所选项目的displayValue。我想提供该功能,同时防止禁用选定的项目。 – CharithJ 2011-05-03 02:08:08