1. Use CustomSource as the prompt set
Add the following code to the window loading function. Assume that unitNameList is the obtained string list to be added to the drop-down list.
AutoCompleteStringCollection collection = new AutoCompleteStringCollection ();
// Obtain the unit list
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;
The AutoCompleteMode includes None, Suggest, Append, and SuggestAppend.
None: Disable auto-completion.
Suggest: Expand the drop-down list and display matching results
Append: auto-completion
SuggestAppend: the combination of Suggest and Append. This means that the drop-down list is automatically completed.
2. directly use the items in the drop-down list as the matching set
Set AutoCompleteSource to ListItems.
// Obtain the unit list
List <string> unitNameList = this. getAllUnitName ();
Foreach (string unitname in unitNameList)
{
This. comboBox2.Items. Add (unitname );
}
This. comboBox2.AutoCompleteSource = AutoCompleteSource. ListItems;