I am trying to implement search suggestions in WinForm. I placed a TextBox on the form; set AutoCompleteMode as SuggestAppend; AutoCompleteSource as CustomSource;
I have implemented TextChanged on the textbox to fetch results and add to AutoCompleteCustomeSource property. If I debug, I could see the results in this property but UI is not showing up any results. My code is here:
private void txtSearchKey_TextChanged(object sender, EventArgs e)
{
AutoCompleteStringCollection searchSuggestions = new AutoCompleteStringCollection();
// Clear current source without calling Clear()
for (int i = 0; i < txtSearchKey.AutoCompleteCustomSource.Count; i++)
txtSearchKey.AutoCompleteCustomSource.RemoveAt(0);
if (txtSearchKey.Text.Length >= 3)
{
var filteredSearchText = String.Join(",", txtSearchKey.Text.Split(' ').Where(x => x.Length > 2));
//The below method retrieves the DataTable of results
var searchResults = MyMethodFetchesResults(filteredSearchText);
if (searchResults.Rows.Count == 0)
{
searchSuggestions.Add("No result found");
}
else
{
foreach (DataRow searchResult in searchResults.Rows)
searchSuggestions.Add(searchResult["Name"].ToString());
}
}
txtSearchKey.AutoCompleteCustomSource = searchSuggestions;
}
Can anyone please throw some light on this? Thanks