Click here to Skip to main content
15,896,489 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,
am raveesh currently working in c#.net application,am using textbox in this i used this code,it is working and shows correctly but i met another problem that i can't select the listed values using arrow key but i can select the listed value using mouse pls help me
C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                AutoCompleteStringCollection col = new AutoCompleteStringCollection();
                string ss = "select name from ss where name like '" + textBox1.Text + "%'";
                SqlCommand cc = new SqlCommand(ss, DbConnection.mCon);
                SqlDataReader dr = null;
                dr = cc.ExecuteReader();
                while (dr.Read())
                {
                    col.Add(dr["name"].ToString());
                }
                dr.Close();
                textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
                textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBox1.AutoCompleteCustomSource = col;
            }
            catch
            {

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DbConnection.Connect();
        }

windows application developer
Posted
Updated 21-Nov-13 23:11pm
v2

1 solution

Why are you doing this in the TextChanged event?
All you are doing is trying to reload the list of sources from...the list of sources...
Move your code, and remove the WHERE clause, and it'll work:
C#
private void textBox1_TextChanged(object sender, EventArgs e)
{
}

private void Form1_Load(object sender, EventArgs e)
{
    DbConnection.Connect();
    AutoCompleteStringCollection col = new AutoCompleteStringCollection();
    string ss = "select name from ss";
    SqlCommand cc = new SqlCommand(ss, DbConnection.mCon);
    SqlDataReader dr = null;
    dr = cc.ExecuteReader();
    while (dr.Read())
    {
        col.Add(dr["name"].ToString());
    }
    dr.Close();
    textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
    textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    textBox1.AutoCompleteCustomSource = col;
}
 
Share this answer
 
Comments
ravuravu 22-Nov-13 5:48am    
hello original grioff,in ma project i need to put the code in texchanged event also the where clause cannot be removed because it is important in ma project u are seeing some code in ma project so that u think like that and sorry to u pls help me
OriginalGriff 22-Nov-13 5:58am    
That's not how autocomplete works - you set the range of values the textbox can show and let it do the work. If you change the range of values it can display based on the value it is currently displaying then it "resets" the textbox, which is why it doesn't work too well.
Try it: it'll probably do exactly what you want right out of the box!
ravuravu 22-Nov-13 6:01am    
k
OriginalGriff 22-Nov-13 6:07am    
You're welcome!

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