C implements ComboBox to automatically match characters

  • 2020-05-09 19:06:54
  • OfStack

1. Use CustomSource as the prompt set
Add the following code to the window loading function. Suppose unitNameList is the list of strings you want to add to the drop-down list.
 
AutoCompleteStringCollection collection = new AutoCompleteStringCollection(); 
//  Get the list of units  
List<string> unitNameList = this.getAllUnitName(); 
foreach (string unitname in unitNameList) 
{ 
collection.Add(unitname); 
//Console.WriteLine(" Automatic prompt " + unitname); 
} 
this.comboBox2.AutoCompleteCustomSource = collection; 
this.comboBox2.AutoCompleteSource = AutoCompleteSource.CustomSource; 
this.comboBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend; 

AutoCompleteMode includes None, Suggest, Append and SuggestAppend.
None: turn off auto completion
Suggest: expand the drop-down list and display the matching results
Append: auto complete
SuggestAppend: a combination of Suggest and Append that displays a drop-down list and is automatically completed.

2. Directly use the items in the drop-down list as the matching collection
AutoCompleteSource is set to ListItems.
 
//  Get the list of units  
List<string> unitNameList = this.getAllUnitName(); 
foreach (string unitname in unitNameList) 
{ 
this.comboBox2.Items.Add(unitname); 
} 
this.comboBox2.AutoCompleteSource = AutoCompleteSource.ListItems; 

Related articles: