1

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

leppie
  • 115,091
  • 17
  • 196
  • 297
Krishna
  • 688
  • 1
  • 7
  • 10

3 Answers3

1

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

var cc = new AutoCompleteStringCollection();
cc.AddRange(listBox1.Items.Cast<string>().ToArray());
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
0

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);
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

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

Another similar question C# AutoComplete

Community
  • 1
  • 1
CharithJ
  • 46,289
  • 20
  • 116
  • 131