Listbox selected Indexes and how to use them

I’ve got a list box that’s populated by the user, I want to enter the selected items into an array, however I need both the indexes they have in the listbox as a whole, as well as their values.

For example, let’s say that there is a listbox with 3 values and the user has selected just the first 2.

I would like to do a “for each” statement on only the selected items, so I would need their listbox indexes, the selected items indexes and their textual values.

I’ve tried multiple paths but all seem to just give me the textual representation of the selected item, but getting the correct indexes seems to be a problem for me.

Any help would be appreciated.


I suggest, you better store the index and the texttual value of the item being selected in the listbox into your array at the same time. And so therefore, there must be a formatting scheme for the index of the item, say for example, format the index value into three digits. For example at index 1 of your listbox, it has a value of “banana”, then, you have to format the index value and concatenate it to its textual value. As a result, it would be, “001banana”, then, make some…string manipulation with it, maybe, substring method will help.

I would suggest using a Dictionary<int,string> to save it. Or a custom class that has value and index in then use a List<CustomItem>