开发者

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

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开发者_如何学Python 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!


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


Check these ones out:

  • Create an AutoComplete TextBox Control in C#
  • AutoComplete TextBox
  • AutoComplete From Database

Hope this helps.


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;


For auto complete textbox at first take a textbox and a listbox with same width.Then create some event such as textchanged,previewtextKeydown,listbox selectionchanged.And u get your solution. For more details please see this example
autocompleteTextBox

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜