簡體   English   中英

再次關於combobox和C#自動完成

[英]Again about combobox and autocomplete on c#

請幫助我制定方法,當用戶在ComboBox中鍵入一些單詞時,可以工作DropDownList並提供具有所有匹配項的列表,而不僅僅是首字母(標准方法自動完成)

我嘗試自己編寫它,但是控件的行為不如使用AutoCompleteMode時好

我2個月都做不到,在所有網頁中都找到了它,尋找了codeproject,而且似乎已經很長時間了,我發現此站點是一種使用自動完成的方法,但是在更改API之前。

對不起,我的語言(谷歌翻譯幫助了我)

PS我正在使用WinForms

你可以做這樣的事情...

從ComboBox的KeyPress事件處理程序中調用函數AutoComplete。

 AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)

 // AutoComplete
public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e)
{
      this.AutoComplete(cb, e, false);
}

public void AutoComplete(ComboBox cb,System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)
{
     string strFindStr = "";

     if (e.KeyChar == (char)8) 
     {
         if (cb.SelectionStart <= 1) 
         {
             cb.Text = "";
             return;
         }

        if (cb.SelectionLength == 0)
            strFindStr = cb.Text.Substring(0, cb.Text.Length - 1);
        else 
           strFindStr = cb.Text.Substring(0, cb.SelectionStart - 1);
     }
    else 
    {
         if (cb.SelectionLength == 0)
            strFindStr = cb.Text + e.KeyChar;
         else
             strFindStr = cb.Text.Substring(0, cb.SelectionStart) + e.KeyChar;
     }

   int intIdx = -1;

    // Search the string in the ComboBox list.

     intIdx = cb.FindString(strFindStr);

     if (intIdx != -1)
     {
             cb.SelectedText = "";
             cb.SelectedIndex = intIdx;
             cb.SelectionStart = strFindStr.Length;
             cb.SelectionLength = cb.Text.Length;
             e.Handled = true;
      }
      else
      {
           e.Handled = blnLimitToList;
       }
  }

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM