![](/img/trans.png)
[英]How to make an auto-complete textbox in a winforms desktop application
[英]How to make a google-like auto complete textbox with C# in a WPF application?
我試圖找到一個解決方案,但我找不到我要搜索的內容。 所以這是我的問題。 我想要一個帶文本框的谷歌行為。 當我輸入“dum”時,它應該在數據庫中找到dummy並將其顯示為文本框下的選項。 它應該是可選擇的。 我不使用ASP.net或任何其他東西。 只是純粹的C#。
謝謝你的幫助!
將AutoCompleteSource
屬性設置為字符串列表,並將AutoCompleteSource
設置為CustomSource
,將AutoCompleteMode
為Suggest
。
我的解決方案
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;
對於自動完成文本框,首先取一個文本框和一個寬度相同的列表框。然后創建一些事件,如textchanged,previewtextKeydown,listbox selectionchanged。你得到你的解決方案。 有關詳細信息,請參閱此示例
autocompleteTextBox
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.