Results 1 to 3 of 3

Thread: HitTest property for a ListBox?????

  1. #1
    appi101
    Guest

    HitTest property for a ListBox?????

    Hi

    Is there anyway of finding out from the x& y co-ord's of a point the position of it in terms of listindex for a listbox. If the user drags something over the listbox is it possible to insert the data in between the 2 items? Something like the HitTest property which other controls have.

    Appi

  2. #2
    Addicted Member Geoff Gunson's Avatar
    Join Date
    Jun 1999
    Posts
    241
    Do you have Microsoft Windows Common controls??

    If so you can use a ListView control with the style set to list.

    This control supports hittest functionality.

    Code:
    Private Sub ListView1_DragDrop(Source As Control, x As Single, y As Single)
        MsgBox ListView1.HitTest(x, y)
    End Sub
    HTH

    G

  3. #3
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    VB Code:
    1. 'in a module
    2. Option Explicit
    3.  
    4. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    5.  
    6. Public Const LB_ITEMFROMPOINT As Long = &H1A9
    7.  
    8. Public Function MakeLong(ByVal intLow As Integer, ByVal intHigh As Integer) As Long
    9.    Dim strLow As String
    10.    strLow = "0000"
    11.    'format the low word as a 4 digit hex string
    12.    Mid$(strLow, 5 - Len(Hex$(intLow))) = Hex$(intLow)
    13.    'return the new long
    14.    MakeLong = CLng("&H" & Hex$(intHigh) & strLow)
    15. End Function
    16.  
    17. Public Function IndexFromXY(ByVal hWndListBox As Long, ByVal X As Single, ByVal Y As Single) As Integer
    18.    Dim lngXY As Long
    19.    'put the X,Y position (in pixels) into a long as the function expects
    20.    lngXY = MakeLong(CInt(X \ Screen.TwipsPerPixelX), CInt(Y \ Screen.TwipsPerPixelY))
    21.    'get the index.  the low word of the return is the closest item
    22.    'in the listbox, but only the low word is important
    23.    IndexFromXY = SendMessage(hWndListBox, LB_ITEMFROMPOINT, 0, lngXY) And &HFFFF
    24. End Function
    25.  
    26. 'usage
    27. Private Sub lstItems_DragDrop(Source As Control, X As Single, Y As Single)
    28.    Dim intDropIndex As Integer
    29.    intDropIndex = IndexFromXY(lstItems.hWnd, X, Y)
    30. End Sub
    Last edited by Kaverin; Aug 14th, 2001 at 06:07 PM.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width