By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,742 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,742 IT Pros & Developers. It's quick & easy.

Listbox - position a certain row at top of displayed window

P: n/a
Hope someone can help.

I have a listbox displaying time periods in blocks of 15 mins for a 24
hour period, all up 96 rows.

The listbox can only visibly show 20 rows a time.

The default feature is that the first row is at the top of the listbox.
I'm wanting to, by default, have the listbox display from row 40
onwards. I can have row 40 selected but it doesn't scroll the rows
down so as row 40 is at the top of the listbox window.

Hope the above makes sense, any suggestions welcome.

Steve.

Sep 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Here's two methods:
--------------------------------------------------------------
Method 1
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


--------------------------------------------------------------
Method 2
Here's the code to force a ListBox to Scroll to a specific row. I put it
behind a Command Button Named Customer, you can obviously do
whatever you want. Really should be a Class Wrapper for a ListBox to expose
a TopIndex property like VB ListBoxes.

' ***CODE START
'Place this code in the General Declarations of your Form
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long

Private Declare Function GetFocus Lib "user32" () As Long

' Windows Message Constant
Private Const WM_VSCROLL = &H115
' Scroll Bar Commands
Private Const SB_THUMBPOSITION = 4
' Code end for General Declarations
' Code for Control's Click Event
Private Sub Customer_Click()

Dim hWndSB As Long
Dim lngRet As Long
Dim lngIndex As Long
Dim LngThumb As Long

' You will get lngIndex value from the user or whatever.
' For now I'm just setting it to arbitrary Number
lngIndex = 45

' SetFocus to our listBox so that we can
' get its hWnd
Me.List2.SetFocus
hWndSB = GetFocus

' Set the window's ScrollBar position
LngThumb = MakeDWord(SB_THUMBPOSITION, CInt(LngIndex))
lngRet = SendMessage(hWndSB, WM_VSCROLL, LngThumb, 0&)

End Sub

' Here's the MakeDWord function from the MS KB
Function MakeDWord(loword As Integer, hiword As Integer) As Long MakeDWord =
(hiword * &H10000) Or (loword And &HFFFF&) End Function '***END CODE

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"superjacent" <su*********@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
Hope someone can help.

I have a listbox displaying time periods in blocks of 15 mins for a 24
hour period, all up 96 rows.

The listbox can only visibly show 20 rows a time.

The default feature is that the first row is at the top of the listbox.
I'm wanting to, by default, have the listbox display from row 40
onwards. I can have row 40 selected but it doesn't scroll the rows
down so as row 40 is at the top of the listbox window.

Hope the above makes sense, any suggestions welcome.

Steve.

Sep 20 '06 #2

P: n/a
I assume you're automatically selecting row 40 by means of some event
code. What happens if you have it select row 50?

HTH
--
-Larry-
--

"superjacent" <su*********@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
Hope someone can help.

I have a listbox displaying time periods in blocks of 15 mins for a
24
hour period, all up 96 rows.

The listbox can only visibly show 20 rows a time.

The default feature is that the first row is at the top of the
listbox.
>

I'm wanting to, by default, have the listbox display from row 40
onwards. I can have row 40 selected but it doesn't scroll the rows
down so as row 40 is at the top of the listbox window.

Hope the above makes sense, any suggestions welcome.

Steve.

Sep 20 '06 #3

P: n/a
Thanks Stephen

I've opted for method 1. Much appreciated.

Steve.

Sep 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.