2
votes

I have a specific problem with a combobox in visual studio. I use it to let the user type text in the textbox part of the combobox which immediately starts a SQL requst. The result should be displayed in the dropdownlist part of the combobox. (DropDownStyle is set to DropDown)

private void UpdateParent(object sender, EventArgs e)
        {
            ParentListChange(); //Update the listitems

            //prevent from opening at the beginning
            if (!ParentSelect.Text.Trim().Equals("")) 
            {
                //my problem
                ParentSelect.DroppedDown = true;

                Cursor.Current = Cursors.Default;
            }
        }

But as soon as the drop down opens the first item gets selected and the whole text of it will paste into the textbox. So if you start to write more then one letter in a row the first one "disappears" because the second typed letter replaces the selected text.

I know there is a similar post but the answeres did not help since they would be to slow (user would have to wait for about a second to type on):

private void comboBox1_TextUpdate(object sender, EventArgs e)
    {
        var savedText = comboBox1.Text;
        comboBox1.DroppedDown = true;
        comboBox1.Text = savedText;
        comboBox1.Select(savedText.Length, 0);
    }

Or would not worke with opening the drop down list, which is essential:

comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

Is there a way to just disable the "select first item" thing?

1
What is your question about? WPF? UWP? WinForms? Web Forms? Xamarin? Something else? Use tags!György Kőszeg
Where is your DropDownList? Where is your TextBox? The only control visible in your code is a ComboBox. Please try to put more effort into describing your problem accurately.Sabuncu
A combobox is composed of a textbox and a dorpdownlist. But i can add that only that one element is used.Luy

1 Answers

1
votes

One way to achieve is to have custom user control.

You can have a TextBox overlapping your ComboBox.

So, user will be interacting with Textbox only, and as per text entered in TextBox(using appropriate Event - key press will be fine), you can query in database and load ComboBox with the result of query.