Results 1 to 5 of 5

Thread: Right Mouse Click Select Listbox Item [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Resolved Right Mouse Click Select Listbox Item [RESOLVED]

    I have a listbox with a bunch of entries. I have a right mouse click popup menu which does some things, one of which is to delete the selected item. The problem is that I have to left mouse click to select the item, then right mouse click to popup my menu. How can I select the item and popup my menu all with one right mouse click?
    Last edited by SeanK; Jan 18th, 2005 at 12:34 PM.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Right Mouse Click Select Listbox Item

    Try this quicky and let me know:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const LB_ITEMFROMPOINT As Long = &H1A9
    4.  
    5. Private Declare Function SendMessage Lib "User32.dll" Alias "SendMessageA" _
    6.     (ByVal hwnd As Long, ByVal wMsg As Long, _
    7.      ByVal wParam As Long, lParam As Any) As Long
    8.  
    9. Private Sub List1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    10. Dim lngRetValue As Long
    11. Dim lngIndex As Long
    12. Dim lngCurPos As Long
    13.  
    14. On Error Resume Next
    15.  
    16.     If Button = vbRightButton Then
    17.         lngCurPos = (ScaleX(x, vbTwips, vbPixels)) Or (ScaleY(y, vbTwips, vbPixels) * &H10000)
    18.         lngRetValue = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0&, ByVal lngCurPos)
    19.         lngIndex = lngRetValue And &HFFFF
    20.         List1.ListIndex = lngIndex
    21.         If Not List1.ListIndex < 0 Then
    22.             MsgBox List1.List(List1.ListIndex)
    23.         End If
    24.     End If
    25.  
    26. End Sub

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Right Mouse Click Select Listbox Item

    RhinoBull and I are on the same page with this one. Fundamentally, the only difference is that I added a form level variable to hold the index of the selected listbox item so it could be deleted from a popup menu event and used a difference constant.
    VB Code:
    1. Option Explicit
    2.  
    3. 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
    4.  
    5. Private Const LB_GETITEMRECT = &H198
    6. Private Const LB_ERR = (-1)
    7.  
    8. Private Type RECT
    9.     Left As Long
    10.     Top As Long
    11.     Right As Long
    12.     Bottom As Long
    13. End Type
    14.  
    15. Private iItemIndex As Integer
    16.  
    17. Private Function GetRClickedItem(MyList As Control, X As Single, Y As Single) As Long
    18. 'Determine which item was right clicked in a list box.
    19. 'Call this from the mousedown event of the listbox and pass the
    20. 'X and Y values from that control.
    21.  
    22.     Dim lClickX As Long
    23.     Dim lClickY As Long
    24.     Dim lRet As Long
    25.     Dim CurRect As RECT
    26.     Dim i As Long
    27.  
    28.     'get x and y
    29.     lClickX = X \ Screen.TwipsPerPixelX
    30.     lClickY = Y \ Screen.TwipsPerPixelY
    31.  
    32.     'Check all items in the list to see if it was clicked on
    33.     For i = 0 To MyList.ListCount - 1
    34.       'get current selection as rectangle
    35.       lRet = SendMessage(MyList.hwnd, LB_GETITEMRECT, i, CurRect)
    36.       'If the position of the click is in the this list item
    37.       'then that's  our Item
    38.      If (lClickX >= CurRect.Left) And (lClickX <= CurRect.Right) _
    39.        And (lClickY >= CurRect.Top) And _
    40.           (lClickY <= CurRect.Bottom) Then
    41.             GetRClickedItem = i
    42.             Exit Function
    43.         End If
    44.     Next
    45.  
    46. End Function
    47. Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    48. If Button = vbRightButton Then
    49.    iItemIndex = GetRClickedItem(List1, X, Y)
    50.      If iItemIndex <> -1 Then
    51.         List1.ListIndex = iItemIndex
    52.         PopupMenu mnuFile
    53.      End If
    54. End If
    55. End Sub
    56.  
    57. Private Sub mnuDelete_Click()
    58. List1.RemoveItem iItemIndex
    59. End Sub

  4. #4

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Right Mouse Click Select Listbox Item [RESOLVED]

    RhinoBull, Hack - thank you both!
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Right Mouse Click Select Listbox Item [RESOLVED]

    NO problem, you're welcome.

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