2013-01-31 146 views
1

我有一個自定義組合框,繼承窗體Forms.ComboBox類。它的DataSource被綁定。設置ComboBox.DroppedDown = false重置所選項目

this.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest; 
this.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems; 
this.DropDownStyle = ComboBoxStyle.DropDown; 

OnDropDown其AutoCompleteMode設置爲無,然後,在OnDropDownClosed,它被設置爲再次建議(以避免這裏desctibed WinForms ComboBox DropDown and Autocomplete window both appear問題)

我: 其屬性如下初始化一個實現的邏輯,用於驗證組合框,並在輸入某些文本時選擇第一個匹配項目。

當顯示DropDown並且用戶輸入某些內容並按Enter時,第一個匹配項被選中。然後我想關閉DropDown。我通過將ComboBox.DroppedDown屬性設置爲false。問題是,當DropDown以這種方式關閉時,所選項目重置爲先前選擇的項目。 OnSelectedIndexChanged,OnSelectionChangeCommitted和OnSelectedValueChanged不會被調用。

我通過記住在OnSelectedIndexChanged中選擇的索引並在OnDropDownClosed(在手動關閉DropDown的條件)中再次設置它來處理它。

問題: 爲什麼選擇的項目,索引,文本和值重置當DroppedDown配件的ComboBox設置爲false?是否有另一種手動關閉ComboBox的方法,但沒有此副作用?

編輯:代碼ilustrate問題:

List<string> list = new List<string>(); 
      list.Add("item0"); 
      list.Add("item1"); 
      list.Add("item2"); 
      list.Add("item3"); 
      list.Add("item4"); 

      customComboBox1.DataSource = list; 

...

public class CustomComboBox : ComboBox 
    { 

     public CustomComboBox(){ } 

     #region OnKeyDown 
     protected override void OnKeyDown(KeyEventArgs e) 
     { 
      if (!this.Enabled) e.Handled = true; 
      else 
      { 
       base.OnKeyDown(e); 
       if (e.KeyCode == Keys.Enter) 
       { 
        this.SelectedIndex = 2; // index changes here 

        Console.WriteLine("ENTER - before DroppedDown=false " + 
         this.SelectedIndex + " " + this.Text); 

        if (this.DroppedDown) 
        { 
         // here index = 2, text = Item2 
         this.DroppedDown = false; 
         // here index = previous index, text is still Item2 
         // different thing is displayed than is actually chosen 
         // OnSelectedIndexChanged wasn't raised 
        } 

        Console.WriteLine("ENTER - after DroppedDown=false " + 
         this.SelectedIndex + " " + this.Text); 

        e.Handled = true; 
       } 
      } 
     } 
     #endregion 

     #region OnSelectedIndexChanged 
     protected override void OnSelectedIndexChanged(EventArgs e) 
     { 
      Console.WriteLine("OnSelectedIndexChanged " + 
       this.SelectedIndex + " " + this.Text); 

       base.OnSelectedIndexChanged(e); 
     } 
     #endregion 

     #region OnDropDownClosed 

     protected override void OnDropDownClosed(EventArgs e) 
     { 
      Console.WriteLine("OnDropDownClosed " + 
       this.SelectedIndex + " " + this.Text); 

      base.OnDropDownClosed(e); 


     } 

     #endregion 

    } 

控制檯輸出如下(選擇項:0,試着同時按下Enter鍵更改爲2 ,關閉DropDown指數後仍爲0):

OnSelectedIndexChanged 0 item0

OnSelectedIndexChanged 2 ITEM2

ENTER - 前DroppedDown = FALSE 2 ITEM2

OnDropDownClosed 0 ITEM2

ENTER - 後DroppedDown =假0 ITEM2

回答

1
protected override void OnDropDownClosed(EventArgs e) 
    { 
     Console.WriteLine("OnDropDownClosed " + 
      this.SelectedIndex + " " + this.Text); 


     int idx = this.SelectedIndex; 

     base.OnDropDownClosed(e); 

     this.SelectedIndex = idx; 


    } 
+0

其實在問這個問題之前,我已經這麼做了。我想知道爲什麼會發生這種情況,以及是否有另一種解決方法。但由於沒有人回答,而且我最終還是做了這件事,所以我將其標記爲答案。 – Arie

0

我已經發現設置SelectedItem比設置SelectedIndex更可靠。

//這將改變SelectedIndex,並觸發SelectedItemChanged事件。

this.SelectedItem = this.Items [idx];