0
votes

Can anyone tell me how can I see the selected order of intems in a listbox in C#? For example, if I have this elements in the listbox:

Item1 Item2 Item3 Item4 Item5

and I select in this order Item4, Item2 and Item5, I need a way to find how that the items were selected in the mentioned order.

Thank you!

2
What framework (WPF, WinForms)? Though I doubt that any of them provides such functionality out of the box.Eugene Podskal
WindowsForms frameworkȘtefan Blaga
@Stefan Then edit your question to include the appropriate tag (winforms).Eugene Podskal

2 Answers

1
votes

Would probably get the index of the item selected and add that to an Array perhaps?

0
votes

Expanding a little in Rudolf's idea, using a List<T>:

List<int> selected = new List<int>();

You can keep track of the items so far selected:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // add new selection:
    foreach (int index in listBox1.SelectedIndices) 
            if (!selected.Contains(index)) selected.Add(index);
    // remove unselected items:
    for (int i = selected.Count - 1; i >= 0; i-- ) 
        if (!listBox1.SelectedIndices.Contains(selected[i])) selected.Remove(i);
}

To test you can write:

for (int i = 0; i < selected.Count; i++) 
   Console.WriteLine("Item # " selected[i] + ": " + listBox1.Items[selected[i]]);

Note that when you use the expanded multiselection option you will get the usual, slightly weird Windows selection order..