[英]How can I get a dynamically generated drop down choices for a text field in C#?
我想在C#winform應用程序中有一個文本字段,而我想要的效果是,當用戶在文本字段中鍵入內容時,該程序在后台搜索數據庫,然后為用戶生成一個下拉列表以從中選擇 ..
這是兩個基於Web的示例,但請注意,我的應用程序是基於Winforms的,而不是基於Web的..我只想要相同的效果,這就是為什么要顯示這些內容:
cinemasquid.com:
blu-ray.com:
如何在C#winform應用程序中的文本字段上獲得相同的效果?
首先,您需要在表單上綁定TextChanged
事件。 然后,當用戶按任意鍵時,您將獲得該事件。 現在,在事件處理程序內部,檢索用戶輸入的字符串,並使用此字符串執行搜索(不要在UI線程上進行搜索,否則UI會掛起,您可以在BackgroudWorker
( MSDN )中進行搜索)。 獲得結果后,將此結果綁定到ListBox
。
我已經開發了一個具有自動完成功能的應用程序。 在這種情況下,如果用戶輸入任何電影名稱,則匹配結果將用於顯示在列表框中。
希望這對您有用。
如果您不想編寫解決方案的代碼,則可以通過Google搜索獲得一些自定義控件,例如: http : //www.dotnetfunda.com/articles/article225.aspx
但是您應該牢記響應時間:如果每次用戶在文本框上輸入字母時對數據庫搜索進行編碼,則應用程序可能會變慢和/或無響應,具體取決於表上的記錄數,網絡連接速度和很多其他因素。
考慮在內存中預加載字符串集合(名稱,標題,以及要在文本框上顯示的內容),然后對該內存中集合執行LINQ查詢以填充控件的自動完成部分。
正如Amar Palsapure所說,您需要使用DropDown的TextChanged
事件。 此外,還將歡迎背景工作者。 這是一個簡短的示例:
首先,我們需要一些數據源。 在這種情況下,這將是一個簡單的字符串列表:
List<string> DataSource = new List<string>
{
" How do I use jQuery to select all children except a select element",
"How can I get the text of the selected item from a dropdown using jQuery?",
"Dropdown menu in IE6 inserting too much width, not dropping-down",
"How to display images within a drop down box instead of text",
"InfoPath 2007 - Populate drop-down list on-the-fly",
"Is there any difference between drop down box and combo box?",
"Drop Down list null value not working for selected text c#?",
"Make drop-downs automatically drop in a loop cycle",
"PHP How can I keep the selected option from a drop down to stay selected on submit?",
"Jquery Issue - Drop down list does NOT show from iPhone/Mobile Devices"
};
接下來是BackgroundWorker DoWork事件-這是在我們的數據源中搜索有效位置的部分響應:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
String text = e.Argument as String;
List<String> results = new List<string>();
//Code for seraching text - may be diffrent if you have another DataSource
foreach (String dataString in DataSource)
{
if (dataString.IndexOf(text, 0, StringComparison.CurrentCultureIgnoreCase) != -1)
{
results.Add(dataString);
}
}
e.Result = results;
}
您可以看到Result由e.Result
返回,因此我們也需要實現RunWorkerCompleted
事件:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
comboBox1.Items.AddRange((e.Result as List<String>).ToArray());
comboBox1.DroppedDown = true;
}
此代碼將填充我們的dropDown女巫返回的值,並將其顯示給用戶。
當然,要進行此運行,您需要在TextChanged
調用RunWorkerAsync
:
private void comboBox1_TextChanged(object sender, EventArgs e)
{
// Save position of cursor, because it like to dissapering.
int cursor = comboBox1.SelectionStart;
//Clearing items in dropDown
comboBox1.Items.Clear();
//Is something was searched before, cancel it!
while (backgroundWorker1.IsBusy)
{
if (!backgroundWorker1.CancellationPending)
{
backgroundWorker1.CancelAsync();
}
}
// And search new one
backgroundWorker1.RunWorkerAsync(comboBox1.Text);
// And bring back cursor to live
comboBox1.SelectionStart = cursor;
}
希望對您有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.