2016-10-24 109 views
0

有沒有辦法改變TextBox類的AutoCompleteStringCollection類的搜索功能?現在,我使用默認搜索功能,其中我的建議的開頭字母與用戶輸入的字母匹配?我的建議來自自定義來源。AutoCompleteStringCollection的自定義搜索

+0

請標記您的演示技術。我們不知道你指的是什麼類型的「文本框」。 –

+0

@ rory.ap。對於那個很抱歉。 –

+0

您沒有指定要查找的是哪種搜索功能。 – LarsTech

回答

0

你可以使自己的自動完成搜索 - 使用TextChange 這裏是答案:C# winforms combobox dynamic autocomplete

代碼:

string[] data = new string[] { 
    "Absecon","Abstracta","Abundantia","Academia","Acadiau","Acamas", 
    "Ackerman","Ackley","Ackworth","Acomita","Aconcagua","Acton","Acushnet", 
    "Acworth","Ada","Ada","Adair","Adairs","Adair","Adak","Adalberta","Adamkrafft", 
    "Adams" 

}; 
public Form1() 
{ 
    InitializeComponent(); 
} 

private void comboBox1_TextChanged(object sender, EventArgs e) 
{ 
    HandleTextChanged(); 
} 

private void HandleTextChanged() 
{ 
    var txt = comboBox1.Text; 
    var list = from d in data 
       where d.ToUpper().StartsWith(comboBox1.Text.ToUpper()) 
       select d; 
    if (list.Count() > 0) 
    { 
     comboBox1.DataSource = list.ToList(); 
     //comboBox1.SelectedIndex = 0; 
     var sText = comboBox1.Items[0].ToString(); 
     comboBox1.SelectionStart = txt.Length; 
     comboBox1.SelectionLength = sText.Length - txt.Length; 
     comboBox1.DroppedDown = true; 
     return; 
    } 
    else 
    { 
     comboBox1.DroppedDown = false; 
     comboBox1.SelectionStart = txt.Length; 
    } 
} 

private void comboBox1_KeyUp(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Back) 
    { 
     int sStart = comboBox1.SelectionStart; 
     if (sStart > 0) 
     { 
      sStart--; 
      if (sStart == 0) 
      { 
       comboBox1.Text = ""; 
      } 
      else 
      { 
       comboBox1.Text = comboBox1.Text.Substring(0, sStart); 
      } 
     } 
     e.Handled = true; 
    } 
}