2012-01-06 50 views
2

我想創建一個文本框,其中自動完成下拉菜單顯示一些建議。
當然,我想在我的文本框上使用AutoCompleteCustomSource,但問題是,文本框會自動過濾所有不包含輸入文本的內容。C#創建自定義自動完成文本框

例如,如果我輸入「listen」,我的算法會將「listen(now)」,「listen(later)」和「listen to AAA」作爲建議。當我把它們放在autocompletectec源碼中時,一切正常。但是,只要我寫「現在」,以便文本變爲「立即聽」,則自動完成下拉列表爲空,因爲自動完成套件源中的所有項都不以「立即聽」開頭。

我接下來嘗試的是將輸入從文本框更改爲組合框,在這裏我將建議放入Items屬性中,然後以編程方式打開下拉列表。這裏的問題是,當我打開代碼下拉菜單時,第一個項目會自動選中,第一個項目的文本會替換輸入的文本。想象一下第一個例子:當你輸入「listen」時,下拉菜單打開項目「listen(now)」,「listen(later)」和「listen to AAA」。但組合框中的文本會自動更改爲第一項,從而變成「listen(now)」,並且不能輸入其他任何內容。

這是我使用的時刻代碼:

private void comboBox2_KeyUp(object sender, KeyEventArgs e) 
    { 
     string asd = comboBox2.Text; 
     if (asd.Length < 3) 
      return; 

     if (e.KeyCode == Keys.Enter) 
     { 
      OpenItem(asd); 
      return; 
     } 
     if (AllToString(comboBox2.Items).Contains(asd)) 
     { 
      return; 
     } 

     DateTime started = DateTime.Now; 
     System.Threading.Thread tth = new System.Threading.Thread((System.Threading.ThreadStart)delegate() 
      { 
       JsonData dat = new JsonData(); 
       //Query autocomplete 
       ... 
       //End Query 
       comboBox2.Invoke((MethodInvoker)delegate() 
       { 
        if (comboBox2.Tag == null || ((DateTime)comboBox2.Tag) < started) 
        { 
         comboBox2.Items.Clear(); 
         comboBox2.Items.AddRange(li.ToArray()); //li is the list of suggestions 
         comboBox2.Select(comboBox2.Text.Length, 0); 
         comboBox2.Tag = started; 
         if (li.Count != 0) 
          comboBox2.DroppedDown = true; 
         else 
         { 
          comboBox2.Focus(); 
          comboBox2.Select(comboBox2.Text.Length, 0); 
         } 
        } 
       }); 
      }); 
     tth.IsBackground = false; tth.Start(); 
    } 

所以我的問題是:我怎麼可以創建一個文本或組合框在那裏我可以把我的建議,在下拉菜單中,在不改變輸入的文本並沒有過濾。我希望所有建議都能始終顯示。

感謝您的幫助,亞歷克斯

+0

使用文本框,嘗試通過檢索總是相同的建議列表,而不用通過用戶輸入過濾它們。 – 2012-01-08 00:36:43

回答

1

更好的是創建一個新的類組合框的哪個herite並重寫事件

public class myCombo : ComboBox 
    { 
     protected override void OnPaint(PaintEventArgs e) 
     { 


      base.OnPaint(e); 
     } 
    } 

我做一些事來改變顯示..要放電網,但很久以前。

嘗試在此搜索。

+0

謝謝,我會試試看。 – alex 2012-03-18 01:02:12