473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Listbox - position a certain row at top of displayed window

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
3 14369
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
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
Thanks Stephen

I've opted for method 1. Much appreciated.

Steve.

Sep 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: R.Wieser | last post by:
Hello All, I'm trying to get a "Virtual Listbox" to work. I've currently got a form, and used CreateWindowExA to create a ListBox with the LBS_OWNERDRAWFIXED and LBS_NODATA flags on it. I've...
2
by: Marc | last post by:
Hi all, I can't figure this out. I want the ability to delete an item in a listbox and leave the window area blank, like it is when the listbox is first created and nothing selected. Right...
7
by: Colleyville Alan | last post by:
I have an app in which users are displayed a list of mutual fund from which they can choose. There is a listbox embedded in a two-tabbed notebook control. When the form is initally opened, the...
1
by: Jay | last post by:
Hi, I have a very large list that I would like to have displayed in a ListBox. Trying to load the entire list takes WAY too long. Is there any kind of buffer I can use to allow part of the list...
2
by: Jared Baszler | last post by:
I just wanted to write and ask why the below behavior is occuring. I've simply put a list box on a form and populated it with two items. I'm handling both the SelectedValueChanged event and the...
7
by: GTi | last post by:
In Win32 I have a function that opens several Dialogs and put the window handle in a listbox (together with a string): HWND hWnd = CreateDialogParam(...) LB_SETITEMDATA, index, (LPARAM)hWnd); ...
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
1
by: Daniel | last post by:
hi, I had an asp:listbox, and everytime i click item inside, the bar automatically go to the top, is there any way to keep the scroll position? I turn on the smartNavigation, it still doesn't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.