Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get index of the first visible item in a list box

Status
Not open for further replies.

CaboB

Programmer
Sep 24, 2007
3
YU
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:
Code:
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
 
Perhaps I'm missing something here, but why can't you use the ListIndex property of the ListBox directly?


Regards,
Mike
 
ListIndex property will give me index of last selected item (multiselect list) but I need to catch index of an item during the MouseDown event. At that moment the item on which user press the mouse is not jet selected therefore ListIndex property doesn't help.

The reason I need all of this is because I'm trying to enable drag&drop capabilities within listbox items so the user can rearrange items. Therefore I need to know indexes of two items corresponding to MouseDown and MouseUp coordinates.
OK, after the MouseUp the item where MouseDown occurred will be selected and I could then use the ListIndex property for that index, but I would still have to catch the index of the item where user want to drop the item (MouseUp coordinates).
 
Sorry, didn't realize it was multiselect.

I'm afraid I have little experience with Access form controls. You might want to try some of the other forums, such as "Microsoft: Access Modules (VBA Coding)" or "Win API (Microsoft)" if you don't get other responses here.


Regards,
Mike
 
Code:
Private Function Get_Selected_Index() As Long
  Dim i As Long, index As Long
  
  For i = 0 To ListBox1.ListCount - 1
     If ListBox1.Selected(i) Then
        index = i
        Exit For
     End If
  Next i
  
  Get_Selected_Index = index
End Function
 
hm, for multiselect listbox this Get_Selected_Index() will just return index of one selected item, the one it first comes across, which will be the one with lowest index. Even for singleselect listbox it would return the index of the item that was selected before current MouseDown event.
Meaning, this can't help me if I want to know the index of an item on which user press the mouse during the OnMouseDown event, therefore before the item becomes selected.

Anyway, thanks for responding

Regards,
Savo
 
>SendMessage with GETTOPINDEX or with GETITEMHEIGHT doesn't work for Access listbox

Correct. The Access 2003 ListBox isn't actually a Windows ListBox so it does not respond to ListBox messages
 
Code:
' Control       :  Simple Multi Select Listbox
' Preconditions :  Only one item can be "dragged and dropped"

Option Compare Database

Private Sub Listbox1_Click()
   Dim i As Integer, index_1 As Integer, index_2 As Integer
   
   For i = 0 To Listbox1.ListCount - 1
      If Listbox1.Selected(i) Then
         index_1 = i
         Exit For
      End If
   Next i
   
   index_2 = Listbox1.ListIndex
   
   MsgBox "Index 1:  " & index_1
   MsgBox "Index 2:  " & index_2
      
   For i = 0 To Listbox1.ListCount - 1
      Listbox1.Selected(i) = False
   Next i
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top