2017-05-22 50 views
1

我有一個班級根據傳遞給它的文本返回Google建議的列表。我的問題是:搜索建議API和自動完成TextBox的

How can I use this class to work as an AutoCompleteCustomSource for a textbox tool so user can write text in it and that text sent to this class to bring a list of Suggestions as user is writing and every litter that user add will be added to the text which will be sent again to the class to bring more Suggestions?

例如,這個調用這個類的填充textbox1基於建議名單「的Arduino」詞:

SearchSuggestionsAPI search = new SearchSuggestionsAPI(); 
IList result = (await search.GetSearchSuggestions("arduino")); 
for (int i = 0; i < result.Count; i++) 
{ 
    string sent = result[i].ToString(); 
    textBox1.AutoCompleteCustomSource.Add(result[i].ToString()); 
    textBox1.AppendText(sent); 
    textBox1.AppendText(Environment.NewLine); 
} 
+3

可能重複[自動完成文本框控件](https:// stackoverflo w.com/questions/1357853/autocomplete-textbox-control) – Sinatr

回答

0

我還沒有嘗試過,但像這樣的東西可能會工作:

private void textBox1_KeyUp(object sender, KeyEventArgs e) 
{ 
    var results = (await search.GetSearchSuggestions(textBox1.Text)); 

    textBox1.AutoCompleteCustomSource.Clear(); 
    textBox1.AutoCompleteCustomSource.AddRange(results); 
} 
+0

我試過了你的代碼,我有一個錯誤,因爲'結果'需要聲明,如果我添加: SearchSuggestionsAPI search = new SearchSuggestionsAPI(); 我在最後一行代碼中出現錯誤,因爲'結果'無法轉換爲字符串[] – khalefa

+0

我遇到的錯誤是因爲我的類返回的不是數組的列表 – khalefa

+0

@khalefa您應該可以將其與像這樣:'Array array = results.Cast ().ToArray();' –