2011-08-06 23 views
0

我想添加項目在一個組合框中,其中2個是隻讀,1個是可編輯項目。我面對的問題是我無法想出一個解決方案來編輯​​選定的可編輯項目.. 我有創建一個組合框,並添加3個項目,並將dropdownStyle設置爲DropDownList .. 任何人都可以幫助我? 謝謝如何在C#中的組合框上具有隻讀和可編輯項目?

回答

1

這並不容易。但可能的是,您可以使用TextUpdate事件來檢測對文本的更改。然後再恢復原來的選擇,Control.BeginInvoke()方便。這個示例表單運行良好,在設計器上放置一個組合框。第二個項目是被保護的,你不能使用DropDownList的

public partial class Form1 : Form { 
    public Form1() { 
     InitializeComponent(); 
     comboBox1.TextUpdate += new EventHandler(comboBox1_TextUpdate); 
     comboBox1.Items.Add(new Content { text = "one" }); 
     comboBox1.Items.Add(new Content { text = "two", ro = true }); 
     comboBox1.Items.Add(new Content { text = "three" }); 
    } 

    private void comboBox1_TextUpdate(object sender, EventArgs e) { 
     int index = comboBox1.SelectedIndex; 
     if (index < 0) return; 
     var content = (Content)comboBox1.Items[index]; 
     if (content.ro) this.BeginInvoke(new Action(() => { 
       comboBox1.SelectedIndex = index; 
       comboBox1.SelectAll(); 
      })); 

    } 

    private class Content { 
     public string text; 
     public bool ro; 
     public override string ToString() { return text; } 
    } 
} 

注意,這種風格不允許編輯。