Problem with ComboBox autocomplete when adding values dynamically
In WinForms application, I have a combobox which I am trying to populate with values based on user input. For example if the user types m
it should show him all the values that starts with the letter m
, but I dont want to add all the values in the beginning because there are a lot of values.
To achieve this, I created an event textchanged
and when a user inputs for example the letter m
my program goes to my database and adds all the values with the letter m
to the combobox.
How can I make it add the values first or make the combobox check again if it should suggest values?
Here is my code:
private void comboBox1_TextChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
for (int i = 0; i < MilkProducts.Length; i++)
{
if (M开发者_如何学GoilkProducts[i].StartsWith(comboBox1.Text))
{
comboBox1.Items.Add(MilkProducts[i]);
}
}
}
It seems you may have to use Win32 API (using PInvoke) here by sending appropriate message to the Combo box to show the search result "after" the event handling is done
Please refer to the below URL and you may find the what you are looking for:
http://msdn.microsoft.com/en-us/library/bb775792(VS.85).aspx
i think problem is you are clearing all the items in ComboBox at comboBox1.Items.Clear()
and then accessing its contents at comboBox1.Text
may be you should try doing it differently. or clear it at the end.
精彩评论