1
Answer

hi

How to compare the data with listbox and textbox?

Answers (1)

0
Photo of Scott Lysle
NA 28.5k 14.4m 16y

Not sure what you mean; here is a way to search for a match and a way to select (highlight) a match:

        // search for a match

        private void button1_Click(object sender, EventArgs e)

        {

            foreach (string s in listBox1.Items)

            {

                if (s == textBox1.Text)

                    MessageBox.Show("Found: " + s);

            }

        }

 

        // select the match

        private void button2_Click(object sender, EventArgs e)

        {

            for(int i=0; i<listBox1.Items.Count; i++)

            {

                if (listBox1.Items[i].ToString() == textBox1.Text)

                    listBox1.SelectedIndex = i;

            }

        }