C # enable ComboBox to automatically match characters

Source: Internet
Author: User

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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.