How to make a google-like auto complete textbox with C# in a WPF application?

13,699

Solution 1

Set the AutoCompleteSource property to a list of strings, and set AutoCompleteSource to CustomSource and AutoCompleteMode to Suggest.

Solution 2

Check these ones out:

Hope this helps.

Solution 3

My solution:

private void tbautocomplete_TextChanged(object sender, EventArgs e)
{
    AutoCompleteStringCollection namecollection = new AutoCompleteStringCollection();
    SqlConnection con = new SqlConnection(@"Data Source=88888;Initial Catalog=contrynames;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT  distinct(person_Firstname+''+person_Lastname) AS name FROM persondetails WHERE name Like '%'+@name+'%'";
    con.Open();
    SqlDataReader rea = cmd.ExecuteReader();
    if (rea.HasRows == true)
    {
        while (rea.Read())
        namecollection.Add(rea["name"].ToString());            
    }
    rea.Close();

    tbautocomplete.AutoCompleteMode = AutoCompleteMode.Suggest;
    tbautocomplete.AutoCompleteSource = AutoCompleteSource.CustomSource;
    tbautocomplete.AutoCompleteCustomSource = namecollection;
Share:
13,699
shevron
Author by

shevron

I'm studying computer science in the Netherlands

Updated on June 05, 2022

Comments

  • shevron
    shevron almost 2 years

    I tried to find a solution but I didn't found what I was searching for. So here is my problem. I want a google like behaviour with a textbox. As I type "dum" it should find dummy in the database and display it as option under the textbox. It should be selectable. I don't use ASP.net or any other stuff. Just pure C#.

    Thanks for your help!