Krishna
Krishna

Reputation: 680

ListBox items as AutoCompleteCustomSource for a textbox

I have populated some items into a listbox using the datasource property. Now I need to set the AutoCompleteCustomSource for a textBox from the items listed in the listbox. Precisely, the DataSource for the ListBox and the AutoCompleteCustomSource for the textBox are same. How can I set the AutoCompleteCustomSource without using the for-loops?
.Net 2.0 only. No support for LINQ

Upvotes: 1

Views: 8067

Answers (3)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

The AutoCompleteStringCollection takes only string[], so it should be like this:

var cc = new AutoCompleteStringCollection();
cc.AddRange(listBox1.Items.Cast<string>().ToArray());

Upvotes: 1

CharithJ
CharithJ

Reputation: 47530

Here is a similar question and answer seems to be appropriate. autocomplete textbox on listbox

Another similar question C# AutoComplete

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66449

If your ListBox is a list of strings, you should be able to do this: (untested)

textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox.AutoCompleteCustomSource.AddRange((List<String>)listBox.DataSource);

Upvotes: 0

Related Questions