Jarred Sumner
Jarred Sumner

Reputation: 1853

Verify Text of item in Listbox is the same one in List<string> C#

I'm trying to get it to verify that it has the same item in the List as the one that's currently selected in the listbox

Why does this code not work, It should work unconditionally because the text generated from the listbox is taken from the List choicetitle

if (RemovePackages_Listbox.Text == choicetitle[RemovePackages_Listbox.SelectedIndex])
            {
                MessageBox.Show("The above code worked!");
            }

Upvotes: 0

Views: 118

Answers (2)

Graviton
Graviton

Reputation: 83254

Try this

if (RemovePackages_Listbox.SelectedItem.ToString() == choicetitle[RemovePackages_Listbox.SelectedIndex])
            {
                MessageBox.Show("The above code worked!");
            }

else
{
    MessageBox.Show("RemovePackages_Listbox.SelectedItem.ToString() is "+RemovePackages_Listbox.SelectedItem.ToString()+" and choicetitle[RemovePackages_Listbox.SelectedIndex] is "+choicetitle[RemovePackages_Listbox.SelectedIndex]);
}

And tell us what you see in the popup messagebox?

Upvotes: 1

Sergio Tapia
Sergio Tapia

Reputation: 41148

RemovePackages_Listbox.SelectedIndex

will return a zero-based index of the selected item in the ListBox.

So you're asking:

If the text displayed in my Listbox is the same as the string in my ChoiceTitle List at position SELECTEDINDEX -

Do this.

Triple check that.

Upvotes: 0

Related Questions