How to make an auto-complete textbox in a winforms desktop application

alex4 picture alex4 · Jan 7, 2011 · Viewed 36.7k times · Source

I have a list of words. The list contains about 100-200 text strings (it's names of metro stations actually).

I want to make an auto-complete textbox. For an example, user press 'N' letter, then an (ending of) appropriate option appear (only one option). The ending must be selected.

How to do that?

PS1: I guess, there is a textbox control with a Property something like this:

List<string> AppropriateOptions{/* ... */}

PS2: sorry for my english. If you didn't understand -> ask me and I will try to explain!

Answer

Joel picture Joel · Aug 13, 2013

Just in case @leniel's link goes down, here's some code that does the trick:

AutoCompleteStringCollection allowedTypes = new AutoCompleteStringCollection();
allowedTypes.AddRange(yourArrayOfSuggestions);
txtType.AutoCompleteCustomSource = allowedTypes;
txtType.AutoCompleteMode = AutoCompleteMode.Suggest;
txtType.AutoCompleteSource = AutoCompleteSource.CustomSource;