views:

45

answers:

3

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!");
            }
A: 
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.

Sergio Tapia
I think he's just using SelectedIndex as the index of choiceTitle. There's nothing wrong with that.
junmats
+1  A: 

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?

Ngu Soon Hui
A: 

What is the data type of choicetitle?

John K