I’m creating an application that allows the user to store information about their classes. There is a button in my application that allows the user to enter information and if it matches any item in the listBox then it should display information about it.
I can only get it to work if I specify a particular item by position (e.g. Items[0]) of the listBox and then convert it to a string. My aim is to compare all items in the listBox.
private void button3_Click(object sender, EventArgs e)
{
if (listBox2.Items[0].ToString() == "PersonalInfo")
{
label.Text = "test";
}
}
You need to loop through all of the items in the list. Try something like this:
An alternate, simpler implementation (which will stop if/when it finds a match instead of continuing to check every item) would be