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?
Re: Right Mouse Click Select Listbox Item
Try this quicky and let me know:
VB Code:
Option Explicit
Private Const LB_ITEMFROMPOINT As Long = &H1A9
Private Declare Function SendMessage Lib "User32.dll" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim lngRetValue As Long
Dim lngIndex As Long
Dim lngCurPos As Long
On Error Resume Next
If Button = vbRightButton Then
lngCurPos = (ScaleX(x, vbTwips, vbPixels)) Or (ScaleY(y, vbTwips, vbPixels) * &H10000)
lngRetValue = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0&, ByVal lngCurPos)
lngIndex = lngRetValue And &HFFFF
List1.ListIndex = lngIndex
If Not List1.ListIndex < 0 Then
MsgBox List1.List(List1.ListIndex)
End If
End If
End Sub
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:
Option Explicit
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 Const LB_GETITEMRECT = &H198
Private Const LB_ERR = (-1)
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private iItemIndex As Integer
Private Function GetRClickedItem(MyList As Control, X As Single, Y As Single) As Long
'Determine which item was right clicked in a list box.
'Call this from the mousedown event of the listbox and pass the
'X and Y values from that control.
Dim lClickX As Long
Dim lClickY As Long
Dim lRet As Long
Dim CurRect As RECT
Dim i As Long
'get x and y
lClickX = X \ Screen.TwipsPerPixelX
lClickY = Y \ Screen.TwipsPerPixelY
'Check all items in the list to see if it was clicked on
For i = 0 To MyList.ListCount - 1
'get current selection as rectangle
lRet = SendMessage(MyList.hwnd, LB_GETITEMRECT, i, CurRect)
'If the position of the click is in the this list item
'then that's our Item
If (lClickX >= CurRect.Left) And (lClickX <= CurRect.Right) _
And (lClickY >= CurRect.Top) And _
(lClickY <= CurRect.Bottom) Then
GetRClickedItem = i
Exit Function
End If
Next
End Function
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
iItemIndex = GetRClickedItem(List1, X, Y)
If iItemIndex <> -1 Then
List1.ListIndex = iItemIndex
PopupMenu mnuFile
End If
End If
End Sub
Private Sub mnuDelete_Click()
List1.RemoveItem iItemIndex
End Sub
Re: Right Mouse Click Select Listbox Item [RESOLVED]
RhinoBull, Hack - thank you both! :thumb:
Re: Right Mouse Click Select Listbox Item [RESOLVED]
NO problem, you're welcome. :wave: