0

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";}
		}

and

void Button1Click(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

2
Contributors
1
Reply
4
Views
5 Years
Discussion Span
Last Post by Mitja Bonca
0
for(int i = 0; i< listBox1.Items.Count; i++)
{
     if(listBox1.Items[i] == textBox1.Text)
     {
         //item found
         break;
     }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.