Monday, March 26, 2012

AutoComplete Winforms search not working if item is one character digit (Bug?)

Textbox properites: autocompletemode = append, autcompletesource = customsource. Assuming the autocompletestringcollection of the textbox are as follows:

James
Philipp
Mariah
Clara
8
Ryan
GregEsguerra

If you type the number 8 in the textbox, the textbox autocomplete does not suggest. As defined, it will search the prefix of the source. So, i guess one character does not have a prefix at all. The workaround for this is to
add a space character after the digit 8. So, "8" becomes "8 ". Below is the code:
Code:
private void PopulateWebsiteName()  
      {  
        try  
        {  
          if (dtWebsiteNames != null) { dtWebsiteNames.Clear(); }  
          dtWebsiteNames = JobsReporting.Scripts.GetWebsiteNamesLocal();  
          if (dtWebsiteNames.Rows.Count > 0)  
          {  
            foreach (DataRow row in dtWebsiteNames.Rows)  
            {  
              if (row[1].ToString().Trim().Length == 1)   
              {  
                row[1] = row[1] + ' '.ToString();  
              }  
              websiteNameCollection.Add(Convert.ToString(row[1]));  
              website_id.Add(row[0].ToString());  
            }  
          }  
          txtWebsiteName.AutoCompleteCustomSource = websiteNameCollection;  
        }  
        catch (Exception e)  
        {   
         MessageBox.Show("Error Loading Website Names",   
         "", MessageBoxButtons.OK, MessageBoxIcon.Error);   
         }  
      }  
GetWebsiteNamesLocal() is the method to retrieve websites from the database.

0 comments:

Post a Comment