Christoph Caina
Christoph Caina

Reputation: 21

C# issues ComboBox and Suggestions from Database

I am using a Winform with a comboBox, which is set to AutoCompleteMode = suggest and AutoCompleteSource = List.

The values for the AutoCompletion are stored within a SQLite Database. Due to the huge amount of data in that table, it doesn't make sense to load all values when the form will be created.

Insted, I wanted to run the Query against the user Input. f.E. the User types "S" -> then the Query should return only the Values "... WHERE x LIKE S%;

This is working fine - with one strange behave, which I haven't figured out yet.

To prevent, that the SQL query will be fired without an empty string, I am using the following code:

    private void cb_City_KeyDown(object sender, KeyEventArgs e)
    {
        if (Globals.UseAutoFillOnCities == true)
        {
            if (!string.IsNullOrEmpty(cb_city.Text))
            {
                foreach (DataRow AutoFillItems in dbAction.GetAutoComplete("CITIES", cb_city.Text).Rows)
                {
                    AutoFillCities.Add(AutoFillItems[0].ToString());

                }
                cb_city.DataSource = AutoFillCities;
            }
        }
    }

Now, to the strange behave of my Form: When I run the Application, and go to my comboBox, I am starting with Typing "S" into it.

But then, the comboBox.Text will be appended by the first match in my Database which is NOT starting with "S" instead, with "A" (like I have done the Query without any string for the LIKE statement.

But also, why does the comboBox is showing this value, instead of just showing the dropdown?

If I delete each Character in the ComboBox and try it again everything is working as expected. It is only the first behave after the form was created.

[EDIT] Thanks @dafie:

Thanks, I have changed this (also this was one of my tries earlier) - but then, the Control also behaves in a strange way.

When I am typing the first letter, nothing happens (it will not show the list with the suggestions). When I then type the second Letter, it will remove the first character I have typed, only the second one is visible, and the comboBox will append with the first suggestion of the Search with "S".

for example, I wanted to Type "St", then I will have t"Saal" in my combobox. (Saal is the first result for the search with "S")

The question is: Why does the ComboBox append the Text, if I only set the AutoComplete Method to "suggest"? And why will my first input be removed?

Upvotes: 1

Views: 244

Answers (3)

Christoph Caina
Christoph Caina

Reputation: 21

I think, I have found a solution which seems to work as I expected :)

private void Form1_Load(object sender, EventArgs e)
    {
        foreach (DataRow AutoFillItems in GetAutoComplete("CITIES", comboBox1.Text).Rows)
        {
            AutoFillCities.Add(AutoFillItems[0].ToString());
        }

        foreach (string item in AutoFillCities)
        {
            comboBox1.AutoCompleteCustomSource.Add(item);
        }

        comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

Unfortunately, it is a bit ugly, using two foreach loops directly behind, but in my latest tests this has worked well.

I will now try it within my other project and if it will have the expected results, I will mark the question as solved.

Thanks for all your help :)

Upvotes: 1

Tony Abrams
Tony Abrams

Reputation: 4673

It's changing your combobox item because you are setting the datasource.

The data source is not what's used for the AutoComplete.

Instead you should be using the AutoCompleteCustomSource.

EX:

List<string> AutoData = new List<string>{ "Anthony", "Aaron", "Adam", "Ben", "Brian", "Charles", "Chuck", "Dan"};

private void comboBox1_TextUpdate(object sender, EventArgs e)
            {
                var items = AutoData.Where(a => a.StartsWith(comboBox1.Text));
                comboBox1.AutoCompleteCustomSource.Clear();
                comboBox1.AutoCompleteCustomSource.AddRange(items.ToArray());
                comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            }

Upvotes: 0

dafie
dafie

Reputation: 1169

I think you fire this method before cb_city.Text is filled with text. You should change cb_city.Text != null to !String.IsNullOrEmpty(cb_city.Text), because your code can check condition when cb_city.Text is set to "".

Upvotes: 0

Related Questions