mohammad reza mohammad reza - 1 year ago 189
C# Question

AutoComplete TextBox Control

I want to have a textbox control that it give suggest and append from a database in win application whit C# 2008 and LINQ .

I do it whit Combobox but I can't do it whit textbox .

How do I do it?

Answer Source

This might not be the best way to do things, but should work:

 this.textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
 this.textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

private void textBox1_TextChanged(object sender, EventArgs e)
    TextBox t = sender as TextBox;
    if (t != null)
        //say you want to do a search when user types 3 or more chars
        if (t.Text.Length >= 3)
            //SuggestStrings will have the logic to return array of strings either from cache/db
            string[] arr = SuggestStrings(t.Text);

            AutoCompleteStringCollection collection = new AutoCompleteStringCollection();

            this.textBox1.AutoCompleteCustomSource = collection;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download