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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting focus on the 'top' ite selected in a multi-select listbox 1

Status
Not open for further replies.

VickyC

Technical User
Sep 25, 2010
206
CA
hi to all

Let's say I have a multi-select listbox (lstChoices) where I make a set of selections. What code can I use to set the focus on the 'top' selection (the one with the lowest listindex)? I can do this by choosing the top selection last, but I'd like code to focus on the top item automatically. (Actually, I need to be able to walk through all the selected items from top to bottom, regardless of the order in which they were selected)

much thanks, Vicky
 
OK - I guess ...ItemsSelected.Item(0) should do the trick. Thanks!
 
I'd like to ask this question again (I'll try to explain it better!)

I have an unbound multi-select listbox (lstChoices) where I make a set of selections. Each selected row is 'blackened', and the LAST row selected is always hilighted with a white dotted box around the black background. (not necessarily the bottom row, but the LAST one selected)

First question - what term is used to describe hilighted LAST SELECTED row. (does it have the focus?)

2nd question - Is there a way to set the hilighteditem selection as the TOP selected row without deselecting the others? (I have code to move selected items up/down in the listbox, but it requires the hilighted selection to be the top/bottom selection.)

Thanks for any clues
Vicky C
 
How are ya VickyC . . .

Whats the point in putting a selected row at the top?

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
hi TheAceMan1 and all

I don't want to put a selected row at the top of the listbox! I want to ensure that the top SELECTED row is the one with the current ListIndex.

Here's what I'm doing. I select a set of rows in lstChoices. When I click cmdUP, I want all of the listbox's selected items to move up by 1 row. My code works perfectly when there is only one row selected. The code also works perfectly if the LAST ROW that I SELECTED is the top row of the selected items (i.e. the selected item with the lowest ListIndex). But... I need to move the top selected item first, then the next item...etc. Otherwise, a selected item can move up and 'collide' with another selected item.

I think I've narrowed the problem to one line of code...

intNewListIndex = Me!lstChoices.ListIndex - 1.

The ListIndex always refers to the position of the LAST SELECTED item. I need it to refer to the TOP ITEM in the set of SELECTED ITEMS. Hope this makes sense :)

Vicky


 

To set the ListIndex value to the top selection (i.e. the selected item with the lowest list index) use this:

Code:
Dim x As Integer

With [i]MyListbox[/i]
  For x = 0 To .ListCount - 1
    If .Selected(x) = True Then
       .SetFocus
       .ListIndex = x
       Exit Sub
       End If
  Next
End With

 
thanks Gammachaser - this almost works, but it produces the same problem I've had with other attempts. Eventually, the loop reaches a value of True for .Selected(x). But, when we reach the line ListIndex = x, the other selected rows are de-selected! The code does set the ListIndex to the top selected item, but I need it to keep the other listbox selections still selected.

Thanks for any clues.
Vicky
 
VickyC . . .

Out of curiosity ... could you answer my post [blue]28 Jul 11 22:49[/blue].

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
hi TheAceMan1 um... I think I did answer your post in my reply of 29 Jul 11 14:40. Vicky
 
VickyC . . .

[blue]Gammachaser[/blue] has you .You just can't set the index as it takes over your multiselections. A function that returns the [blue]top index[/blue] should do:
Code:
[blue]Public Function TopIdx() As Integer
  Dim LBx As ListBox
  
  Set LBx = Me.[purple][B][I]YourListboxName[/I][/B][/purple]
  
   For x = 0 To LBx.ListCount - 1
      If LBx.Selected(x) = True Then
         TopIdx = x
         Exit For
      End If
   Next

End Function[/blue]
I'm curious ... the [blue]ItemsSelected[/blue] collection returns a list of all indexes selected during multiselect. Why do you find it necessary to know the top index?

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top