Reputation: 337
c# winform
I have read multiple articles/suggestions on how to do this, and below is one of the few I have tried, but it's not working. In fact, when user types into the textbox, nothing happens.
private void OperationListForm_Load(object sender, EventArgs e)
{
AutoCompleteStringCollection textBoxCollection = new AutoCompleteStringCollection();
foreach (var item in _oiList) //_oiList is a list of objects
{
textBoxCollection.Add(item.ToString());
}
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = textBoxCollection;
}
I'm new so, If I need to provide more info, please let me know.
Upvotes: 0
Views: 67
Reputation: 1847
A few things you need to double check:
I tested your code and it works for me. This is what I did to check:
Added the following code to the Form1.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitTextBox();
}
void InitTextBox()
{
AutoCompleteStringCollection textBoxCollection = new AutoCompleteStringCollection();
textBoxCollection.Add("Bobby");
textBoxCollection.Add("Billy");
textBoxCollection.Add("Britney");
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = textBoxCollection;
}
}
and here is a screenshot of this working:
Upvotes: 2
Reputation: 6203
First you must create array and add it to AutoCompleteStringCollection
, next set it as data source. You can do it like my sample, its working. Your problem is with your datasource
which you try add. It's list of object you cant do that.
AutoCompleteStringCollection stringCollection = new AutoCompleteStringCollection();
String[] yourArray = new[] {"Cat", "Car", "Dog", "Dinner", "War", "White"};
stringCollection.AddRange(yourArray);
textBox1.AutoCompleteCustomSource = stringCollection;
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
Upvotes: 0