SoFunction
Updated on 2025-03-06

C# implements ComboBox to automatically match characters

1. Use CustomSource as a prompt collection
Just add the following code to the window loading function. Assume that unitNameList is the string list you want to add to the drop-down list.
Copy the codeThe code is as follows:

AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
// Get the unit list
List<string> unitNameList = ();
foreach (string unitname in unitNameList)
{
(unitname);
//("Auto Prompt" + unitname);
}
this. = collection;
this. = ;
this. = ;

Among them, AutoCompleteMode includes four situations: None, Suggest, Append and SuggestAppend.
None: Turn off the automatic completion function
Suggest: Expand the drop-down list and display matching results
Append: Automatic completion
SuggestAppend: The combination of Suggest and Append, that is, the drop-down list is automatically completed.

2. Use items in the drop-down list directly as matching collections
AutoCompleteSource is set to ListItems.
Copy the codeThe code is as follows:

// Get the unit list
List<string> unitNameList = ();
foreach (string unitname in unitNameList)
{
this.(unitname);
}
this. = ;