Hello all.
I have a list control and wish to implement a popup menu on mouse right
click. I want to show a small menu with actions to be performed on the
selected listitem only. I tried to use MouseUp or MouseDown events, but on
any click, the index is -1. Also, the listitem is not properly selected by
right click.
Any workaround on the matter ?
Best regards
Marios
You discovered something that I never tried and are quite
correct in identifying a problem.
This might be a solution to your problem to generate what
the listindex would be:
Private Sub List1_MouseUp(Button As Integer, Shift As
Integer, X As Single, Y As Single)
Label1.Caption = List1.TopIndex + Int(Y / 190)
End Sub
>-----Original Message-----
>Hello all.
[quoted text clipped - 11 lines]
>
>.
>I have a list control and wish to implement a popup menu on mouse right
>click. I want to show a small menu with actions to be performed on the
[quoted text clipped - 3 lines]
>
>Any workaround on the matter ?
Here's a little Api code that that might help. The LB_ITEMFROMPOINT
message fetches the item index for a given position.
'declares...
Public Const LB_ITEMFROMPOINT = &H1A9&
Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
'implementation...
Public Function LbItemFromPos(oLB As ListBox, _
ByVal X As Long, ByVal Y As Long) As Integer
'Returns index of item, or -1 if no item at position.
Dim lXY As Long, lRet As Long
With Screen
'Y in hi word, X in low
lXY = ((Y \ Screen.TwipsPerPixelY) * &H10000) _
+ (X \ Screen.TwipsPerPixelX)
End With
lRet = SendMessage(oLB.hWnd, LB_ITEMFROMPOINT, 0&, ByVal lXY)
If lRet < 65535 Then '(hi word 0 = on an item)
LbItemFromPos = CInt(lRet)
Else 'nowhere
LbItemFromPos = -1
End If
End Function
'ex usage...
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Dim nIndex As Integer
If Button = vbRightButton Then
nIndex = LbItemFromPos(List1, X, Y)
If nIndex >= 0 Then
Debug.Print "RtClick on " & nIndex
'PopupMenu ..whatever
End If
End If
End Sub
-Tom
MVP - Visual Basic
(please post replies to the newsgroup)
Amarios - 30 Sep 2004 06:41 GMT
Tom thanks !
it works well. A note only.
In VB6, in Form Object code where i used it
> Public Const LB_ITEMFROMPOINT = &H1A9&
Must be "Const LB_ITEMFROMPOINT = &H1A9&" - public not allowed
> Public Declare Function SendMessage Lib "user32" _
> Alias "SendMessageA" _
> (ByVal hWnd As Long, ByVal wMsg As Long, _
> ByVal wParam As Long, lParam As Any) As Long
Must "Private Declare ..."
Regards
Marios
> >I have a list control and wish to implement a popup menu on mouse right
> >click. I want to show a small menu with actions to be performed on the
[quoted text clipped - 50 lines]
> MVP - Visual Basic
> (please post replies to the newsgroup)
Tom Esh - 30 Sep 2004 18:04 GMT
>Tom thanks !
>
[quoted text clipped - 4 lines]
>> Public Const LB_ITEMFROMPOINT = &H1A9&
> Must be "Const LB_ITEMFROMPOINT = &H1A9&" - public not allowed
Sorry - should have mentioned that. Declares outside a standard (.bas)
module must be Private. (A common practice is to place them in a bas
module unless they're not likely to be used outside the class/ form.)
-Tom
MVP - Visual Basic
(please post replies to the newsgroup)