F,
From Visual Basic Help…
The ListIndex property contains an index of the selected row in a list. Values of ListIndex range from –1 to one less than the total number of rows in a list (that is, ListCount – 1). When no rows are selected, ListIndex returns –1. When the user selects a row in a ListBox or ComboBox, the system sets the ListIndex value. The ListIndex value of the first row in a list is 0, the value of the second row is 1, and so on.
Hopefully I can explain this so it’s clear…Unfortunately, you can’t use the ListIndex property of ListBox1 because ListBox1 allows multiple selections. In a ListBox that allows multiple selections, ListIndex returns the index of the row that has focus, regardless of whether that row is currently selected.
On the bright side, think of a ListBox list like an array that always has an Option Base of zero (it’s not negotiable). You can get the ‘index’ of the selected item(s) from the list in ListBox1 to populate the array ‘SelArr’ by using the value of your variable ‘i’as the ‘index’:
So, from your example above:
“if I select items 3-6 (but not 1-2 or 7-10) from ListBox1”
After ‘SelArr’ is loaded, its’ data should look like this:
SelArr (the list ‘index’ number of item(s) selected from ListBox1)
1 (Automatically selected the 2nd item from ListBox1 and represents List item 2)
2 (Represents List item 3 that was selected)
3 (Represents List item 4 that was selected)
4 (Represents List item 5 that was selected)
5 (Represents List item 6 that was selected)
Your code will then populate ListBox3 & ListBox4 identically like this:
ListBox3&4 List Text
ListItem 2.Text (Index number = 0; auto-selected the 2nd item from ListBox1)
ListItem 3.Text (Index number = 1)
ListItem 4.Text (Index number = 2)
ListItem 5.Text (Index number = 3)
ListItem 6.Text (Index number = 4)
You can then retrieve the selected items’ ‘index’ in ListBox3 and ListBox4 using the same methodology above if they are ‘multi-select’; if not, use the ListIndex property and add 1 to the ‘index’ (it appears ‘SelArr’ is Option Base 1):
Then loop through ‘SelArr’ records to find the matching original ‘index’ number from ListBox1:
Hope this helps,
theDude
Bookmarks