Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"ListIndex" in MultiSelect Listbox? 1

Status
Not open for further replies.

eksortso

Programmer
Jun 30, 2003
43
US
I don't know if what I'm attempting to do is actually possible, but I'm getting inconsistent behavior.

What I'm trying to do is this: I have a ListBox that I want to make easy-to-use for both mouse users and keyboard users. I've set MultiSelect to Extended, because such a need exists and works well for mouse users. Also, the OnKeyPress event procedure for the ListBox gives keyboard users keystrokes to perform operations on the selected items that mouse users can perform by clicking on CommandButtons. This is good so far.

Now, the ListBox has Row Source Type "Value List", and the operations mentioned above require me to change the RowSource value, then select items from the list.

Here's where it gets complicated. I use an array called SelectList to pass in the indices of the items that I wish to select. If I just set Selected(i) to True for each i in the array, like this:

Code:
Me.lbMyListBox.RowSource = StringOfListItems()
Me.lbMyListBox.Requery

For i = LBound(SelectList) To UBound(SelectList)
    Me.lbMyListBox.Selected(SelectList(i)) = True
Next i

then I get the correct items selected, but the "ListIndex", the one item in the list that is accessible with the keyboard, does not move. It always stays at position 0, even if position 0 isn't in the SelectList array.

I'd like do something like the following, so that the focus is on the very first item in the array. This would make the ListBox easier for the keyboard users to use.

Code:
Me.lbMyListBox.RowSource = StringOfListItems()
Me.lbMyListBox.Requery

[green]'The next line causes an error.[/green]
[red]Me.lbMyListBox.ListIndex = SelectList(LBound(SelectList))[/red]
For i = LBound(SelectList) + 1 To UBound(SelectList)
    Me.lbMyListBox.Selected(SelectList(i)) = True
Next i

I thought that this would work, since I can assign a value to Me.lbMyListBox.ListIndex from the Immediate window and get the desired result. However, when this code runs, I get error code 7777: [tt]"You've used the ListIndex property incorrectly."[/tt] This is probably because MultiSelect is set to Extended. But why can I set ListIndex from the Immediate window, but can't set it in my form's code module? That's a minor question, but it's related to my bigger question.

So, can anybody tell me how I can set the "index" on the ListBox while I select items? Thank you for any help that you can offer.
 
And what about this ?
Me.lbMyListBox.SetFocus
Me.lbMyListBox.ListIndex = SelectList(LBound(SelectList))

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top