Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Suggestion list for a text box updated dynamically and fetched from sql database. When I press down key to select/Scroll item from suggestion list then 1st item is selected only. I can't select 2nd, 3rd, 4th ans so on items from suggestion list. How to resolve it ?

Here is my code........ Suggestion list is every time on textBox_textChanged event.


C#
 private void ItemTextBox_TextChanged(object sender, EventArgs e)
{

     try
    {
    string keyword = ItemDescTextBox.Text.TrimStart();
     SqlCommand cmd = new SqlCommand("SELECT ItemDescription FROM ItemsDetail WHERE ItemDescription LIKE '%" + keyword + "%'", con);
     cmd.CommandType = CommandType.Text;
     SqlDataReader dr = cmd.ExecuteReader();

     List<string> ItemSuggestionsList = null;

     if (dr.HasRows)
     {
         ItemSuggestionsList = new List<string>();
         while (dr.Read())
         {
             string row = "";
             row = Convert.ToString(dr["ItemDescription"]);
             ItemSuggestionsList.Add(row);
         }
         ItemSuggestionsList.TrimExcess();
     }

     if (dr != null)
     { dr.Close(); }

        string[] sugs = ItemSuggestionsList.ToArray();
        ItemSuggestionsList.Clear();

        //Create and initialise object source for AutocompleteStringCollection
        AutoCompleteStringCollection source = new AutoCompleteStringCollection();
        source.AddRange(sugs);
        ItemDescTextBox.AutoCompleteCustomSource = source;
        ItemDescTextBox.AutoCompleteMode = AutoCompleteMode.Suggest;
        ItemDescTextBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
    }
    catch (Exception ex)
    {
        label1.Text="ex source: " + ex.Source +
                          "\nex msg: " + ex.Message +
                          "\nex stk trace: " + ex.StackTrace;
    }

 }
Posted
Updated 27-Jan-14 20:48pm
v2
Comments
Aravindba 28-Jan-14 5:27am    
i think,u need to show list in textbox and select the data form list ? if yes just use autocomplete and then select which data u need,that one appear in same textbox ,u can show the list in textbox based on what u type in texbox

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900