0

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.
The problem that I think that the combobox first sees if it should autocomplete (suggest) values and only after that it adds the values.

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 (MilkProducts[i].StartsWith(comboBox1.Text))
        {
            comboBox1.Items.Add(MilkProducts[i]);
        }
    }
}
Anuroopa Shenoy
  • 383
  • 1
  • 4
  • 7
Eli Braginskiy
  • 2,867
  • 5
  • 31
  • 46
  • Not sure what exactly is the problem.. can you please explain better what happens vs. what you want to happen? – Shadow The GPT Wizard Jun 22 '11 at 11:37
  • 1
    have u tried to do this on combo box key press event ? – Ankur Jun 22 '11 at 11:39
  • I want that when the user writes text into the combobox an array of options will be added to the combobox,each time he adds a letter or changes something it need to delete all previous items and add the new ones,it does that,the problem that the combobox first checks if it need to show anything-and because there are no items(my event runs only after the system event) it doesnt show anything. – Eli Braginskiy Jun 23 '11 at 01:47
  • @Ankur, how am i suppsoed to know which key was pressed? it should be multilangual and when i use the e.keycode it just shows the letter in english...,never mind ,i figured how to do this. – Eli Braginskiy Jun 23 '11 at 02:03
  • @Ankur, when i use keypress it works just as textchanged,first the system event fired of the combo box which checks if there are any items to show- and then my event-that adds the items – Eli Braginskiy Jun 23 '11 at 02:09
  • see if this works for you http://stackoverflow.com/questions/11780558/c-sharp-winforms-combobox-dynamic-autocomplete/41461062#41461062 – Devanathan.S Jan 25 '17 at 07:43

2 Answers2

1

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.

  • no,i need to clear in the begining,how would a clear in the end help? i need to clear all previous items and add the new ones... – Eli Braginskiy Jun 23 '11 at 01:47
1

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

Ankur
  • 33,367
  • 2
  • 46
  • 72