473,382 Members | 1,377 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,382 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 14366
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.