PDA

View Full Version : How to get index of the first visible item in a list box



Cabo
09-25-2007, 12:32 AM
I'm developing aplication in MS Access 2003 where I have a form with one listbox holding more items than can be shown at once. Whenever user click on some item I want to calculate (in mousedown and mouseup event) listindex of that item.

Only way I figure I could do it is dividing Y coordinate by height of one row. That way I can get index of my item relative to the top of listbox window. Problem is when list box is scrolled down and topitem visible is not actualy item with 0 listindex.
I found bunch of VB solutions for this problem using API function SendMessage with LB_GETTOPINDEX as parameter but it just doesn't seem to work with VBA
The Code I'm trying to use goes something like this:


Private Declare Function SendMessage Lib "user32"
Alias "SendMessageA" (ByVal hwnd As Long,ByVal wMsg As Long, ByVal wParam As Integer,
ByVal lParam As Any) As Long
Private Declare Function GetFocus Lib "user32" () As Long
Const LB_GETTOPINDEX = &H18E
Const LB_GETITEMHEIGHT = &H1A1
Private Function MyListIndex(Y As Single) As Integer
DIM intTopItemIndex As Integer
DIM dblItemHeight As Double
intTopItemIndex = SendMessage(GetFocus(), LB_GETTOPINDEX, 0&, 0&)
dblItemHeight = SendMessage(GetFocus(), LB_GETITEMHEIGHT, 0&, 0&)
MyListIndex = intTopItemIndex + Y \ dblItemHeight
End Function

Now, it sems that SendMessage with GETTOPINDEX or with GETITEMHEIGHT doesn't work for Access listbox. Am I right?
In both cases SendMessage returns me 0 as a result.
Itemheight I can calculate approximately and put it as constant and it will work (as long as I don't change fontsize and resolution), but I'm afraid I can't calculate item's listindex without knowing listindex of the first visible item.
Is there a way to make this code, or some other code, to work for me in this particular problem?

thanks in advance
Cabo

OBP
09-25-2007, 04:07 PM
Why calculate it, Access knows the Item number selected?

Cabo
09-25-2007, 05:42 PM
Yes but after the item becomes selected.
During the MouseDown event the item on which user press the mouse is not jet selected and user can move mouse on to another item and then release the mouse (MouseUp event) and I want to know indexes of both items in order to perform drag&drop procedure within listbox items

OBP
09-26-2007, 02:48 AM
Access knows all of the selections of a multi Select List Box.
What are you doinf with the data when you have it?

Cabo
09-26-2007, 09:22 AM
I need drag&drop capability within listbox items.
When user press the mouse (drag start) on any item (call it FirstItem) I want to use MouseDown event to store that item index.
Later on, user will move the mouse and release it over another item (SecondItem).
After that I can use ListIndex property to determine index of the SecondItem but at that moment I have no way to know index of FirstItem if I haven't previously stored it.
Ok, problem still stands for Single Select ListBox since I have found some solution that works for MultiSelect List (I will post it here when I finish with tests)