I'm trying to get the selected items in the order they were selected, the problem occurs when I select items in ASC order, I mean from the top to the bottom, it always return the first selectedIndex or value, and the problem is that items can be selected in any order. The ListBox control has SelectionMode set to MultiSimple, and I've tryied with MultiExtended with no success. Curiously when I select the items in reverse order from the bottom to the top it works perfectly. I've been testing, reading and searching everywhere but without getting it working, so far. The ListBox is bounded to a dataSource.

Working directly with the collection is ok cuz it returns the selectedItems, but I can't get items in the order they were selected.

private void lstEquipos_SelectedIndexChanged(object sender, EventArgs e)
{
    if (lstEquipos.SelectedIndex != -1)
        {
            textBox1.Text = lstEquipos.SelectedIndex.ToString();
        }            
    }
}
2

There are 2 answers

0
Karl Anderson On

Try using SelectedItem instead:

private void lstEquipos_SelectedIndexChanged(object sender, EventArgs e)
{
    // Get the currently selected item in the list box 
    string currentItem = lstEquipos.SelectedItem.ToString();

    // Find the index of the currently selected item in the list box 
    int index = lstEquipos.FindString(cucurrentItemItem);

    textBox1.Text = index.ToString();
}
0
Luis Salazar On

Well, although I haven't found an explanation about why this is happening, here I've found a solution to keep items in the order they were selected, stackoverflow.com/a/305601/2722313