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
Asked
Active
Viewed 7,893 times
1
-
I have used DataTable and DataViews to set the dataSource. It works. – Krishna Oct 03 '11 at 22:13
3 Answers
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
-
ooh, good catch i did it like that because i didn't want to use foreach or for loop. – Mahmoud Gamal Sep 27 '11 at 01:42
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
-
this won't compile, the AutoCompleteStringCollection takes only string[] – Mahmoud Gamal Sep 27 '11 at 01:11
0
Here is a similar question and answer seems to be appropriate. autocomplete textbox on listbox
Another similar question C# AutoComplete