Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageOn Wed, 29 Sep 2004 09:49:57 +0300, "Amarios" <amarioNO-SPAM@in.gr> wrote: >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 ? 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)
Post Follow-up to this messageTom 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 "Tom Esh" <tjeshGibberish@earthlink.net> wrote in message news:clpll0l2d0cdmpr8tmlsdsrcsvkf9shmf8@ 4ax.com... > On Wed, 29 Sep 2004 09:49:57 +0300, "Amarios" <amarioNO-SPAM@in.gr> > wrote: > on by > > 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)
Post Follow-up to this messageOn Thu, 30 Sep 2004 08:41:55 +0300, "Amarios" <amarioNO-SPAM@in.gr> wrote: >Tom thanks ! > >it works well. A note only. > >In VB6, in Form Object code where i used it > > 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)
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.