Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i trying to find values in listbox and highlight that according to textbox value but i am getting this error. I googled but failed to find my answer pls. Help....

Thank You..

to load values in listbox
C#
private void FilterListBox_Load(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Data Source=SRISTI;Initial Catalog=Anjanee;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter("select UserName from User_Name", cn);
            DataSet ds = new DataSet();
            da.Fill(ds, "ABC");
            int count = ds.Tables[0].Rows.Count;
            for (int i = 0; i < count; i++)
            {
                listBox1.Items.Add(ds.Tables[0].Rows[i]["UserName"].ToString());
            }
        }



C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            foreach (string s in listBox1.Items)
            {
                if (s.StartsWith(textBox1.Text) == true)
                {
                    listBox1.SelectedItem = s;
                }
            }
        }
Posted
Comments
Jameel VM 21-Jun-13 2:53am    
where is the problem?
Anjanee Kumar Singh 21-Jun-13 2:54am    
The Above heading is error
Jameel VM 21-Jun-13 2:59am    
i mean which line?
Anjanee Kumar Singh 21-Jun-13 3:00am    
string s after second round

As per my understand you are trying to do the Autocomeplete extender in window application.
Go through the below link for sample program
http://www.mstecharticles.com/2010/12/auto-complete-text-box.html[^]

Thanks
--RA
 
Share this answer
 
You might change from:
Quote:
foreach (string s in listBox1.Items)
{
if (s.StartsWith(textBox1.Text) == true)
{
listBox1.SelectedItem = s;
}
}


to

C#
for(int i=0; i<listbox1.items;>  {
  if (listBox1.items[i].StartsWith(textBox1.Text) == true)
  {
     listBox1.SelectedIndex = i;
  }
}
 
Share this answer
 
 
Share this answer
 
Comments
Anjanee Kumar Singh 21-Jun-13 3:18am    
Thanks to all I found my Mistake through you people's Link.....and replies..


Thank You...

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

  Print Answers RSS


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