2012-05-02 31 views
0

我有一個基本窗體,其上有一個組合框,一個文本框和一個按鈕。組合框內有不可更改的項目數量,但項目本身可以通過爲所選項目輸入新值來更改。組合框項不會更新,即使情況發生變化

enter image description here

從畫面中的例子,如果我輸入一個字符串,如「標識符」,所選擇的項目中,從「ID」組合框變爲「標識符」,如所預期。但是,如果我輸入「id」,則邏輯(下面)正常執行,項目更新,但在視覺上,項目不會從「ID」更改爲「id」。

下面是按鈕

private void btnApply_Click(object sender, EventArgs e) { 

    string newValue = txtNewName.Text; 

    if(string.IsNullOrWhiteSpace(newValue)) { 
     MessageBox.Show("Please input a new column name"); 
     return; 
    } 

    if(cmbHeaderNames.Items.Contains(newValue)) { 
     MessageBox.Show("A column with that name already exists"); 
     return; 
    } 

    cmbHeaderNames.Items[cmbHeaderNames.SelectedIndex] = newValue; 

    txtNewName.Text = ""; 

} 
+1

我相信你想要使用[BindingList ](http://msdn.microsoft.com/en-us/library/ms132679.aspx)作爲DataSource能夠做到這一點。 – Candide

回答

3

我相信,組合框做一些字符串比較的事件處理程序的代碼,因爲下面的代碼示例工程。

if (comboBox1.SelectedItem.ToString().ToUpper() == textBox1.Text.ToUpper()) 
{ 
    comboBox1.Items[comboBox1.SelectedIndex] = string.Empty; 
    comboBox1.Items[comboBox1.SelectedIndex] = textBox1.Text; 
} 

顯然,如果在應用ToUpper()或ToLower()時兩個字符串值不相同,則更新成功應用。

+0

這對我的使用來說已經夠用了。謝謝。 – daniel

相關問題