3
Answers

How to Check if an item Exist in a ListBox

Behrouz Hosseini

Behrouz Hosseini

13y
16.5k
1
Hi
Can you please let me know how I can check if there is a user entered value exist in a listbox? For example if I have a textbox for user to enter a city name and a list of cities how I can check the city name entered in the list box exist in the listbox.I used to methods Contains() and Equals but they didn't work.


void Button1Click(object sender, EventArgs e)
        {
            string val = textBox1.Text;
            listBox1.Items.ToString().Contains(label1.Text){
             label1.Text = listBox1.Items.ToString();
            }
            else{label1.Text = "No such a Item";}
        }

void Button2Click(object sender, EventArgs e)
        {
            string val = textBox1.Text;
            listBox1.Items.ToString().Equals(label1.Text){
             label1.Text = listBox1.Items.ToString();
            }
            else{label1.Text = "No such a Item";}
        }


Thanks for you time

Answers (3)