Vertical Scroll Bar in ListBox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I want to know how to set the Vertical Scroll Bar to
certain positions in a List Box through code.

Any help would be greatly appreciated.

Thank you,
Jeff
 
Hello,

I want to know how to set the Vertical Scroll Bar to
certain positions in a List Box through code.

Any help would be greatly appreciated.

Thank you,
Jeff

It's not the scroll bars that move, it's the row that you select that
moves the scroll bars.

Me!ListBox.selected(x) =true
Where x is whatever row you wish to select.

To always show the last row:
Me!ListBox.Selected(ListBox.ListCount - 1) = True

Where you place the code depends upon what you are doing.


End Sub
 
That works in a regular list box, but what about a list box in "Multi-list"
mode? When I use code to select a row, the row gets selected, but the
records are not adjusted to show it to the user.

Craig
 
See:
http://www.lebans.com/List_Combo.htm#ScrollListbox
Scroll a ListBox to a specific row. Emulates the VB ListBox TopIndex
property. You can alter the code to easily have the selected row display
as the first or last row as well. The example code is placed behind a
Command Button.

' *** CODE START
Private Sub cmdListIndex_Click()
On Error GoTo Err_cmdListIndex_Click

' Always make NumRows an odd number
' if you want selected Row to be in the
' middle of the ListBox.

' NumRows is the number of completely visible rows in the ListBox Const
NumRows = 7
' Row we want displayed in middle of ListBox.
Dim intDesiredRow As Integer

' Arbitrarily select the 24th row.
intDesiredRow = 24
' ListBox must have the Focus
Me.List2.SetFocus
' Force ListBox to start from the top
Me.List2.ListIndex = 1

' Force the Scroll offset we desire
Me.List2.ListIndex = intDesiredRow + (NumRows / 2)
' Now select the row without further scrolling
Me.List2.ListIndex = intDesiredRow

Exit_cmdListIndex_Click:
Exit Sub

Err_cmdListIndex_Click:
MsgBox Err.Description
Resume Exit_cmdListIndex_Click

End Sub
' ***CODE END



--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
Thanks Stephen! That looks like it is exactly what I need. I'll go play
with that code now.

Thanks!

Craig
 
Hello,

I want to know how to set the Vertical Scroll Bar to
certain positions in a List Box through code.

Any help would be greatly appreciated.

Thank you,
Jeff

I wanted to show a simple way to reset the scroll bar back to the top and
show the first item of the listbox:

Me!List2 = Me!List2.ItemData(0)
Me!List2 = Null

What this code does is set the value of the listbox to the very first
line which causes the listbox to scroll up to the top. Then you set the
value to null to clear out any selection. All this is done without
changing the focus on the form! (important for my application)

I was using the selection in one listbox to set the rowsource of a second
listbox. I did not want the focus to be taken away from the first listbox
because the item that was selected in the first listbox would not be
indicated. It was driving me crazy trying to figure out how to reset the
second listbox without changing the focus. Every time the user was
selecting a new value in the first listbox the second listbox would stay
selected to the last value that was picked, and sometimes it would be
scrolled way down near the bottom of the list, which was confusing for my
users.

This method seemed to solve my problem. I hope that this tip saves
people from the frustration I experienced.

-Wapeka
 
Back
Top