2011-05-03 181 views
0

只有在某些情況下,是否有任何方法可以防止更改ComboBox中的選定項目?我想允許更新ComboBox中所選項目的displayValue。但我不希望用戶在更新時更改所選項目。這是一個Windows應用程序。已選擇項目更改組合框

回答

1

你的類中:

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

0

嘗試設置將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

相關問題