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!
将AutoCompleteSource
属性设置为字符串列表,并将AutoCompleteSource
设置为CustomSource
,将AutoCompleteMode
为Suggest
。
Check these ones out:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.