Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General / September 2004



Tip: Looking for answers? Try searching our database.

List control MouseDown or MouseUp does not return index

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amarios - 29 Sep 2004 07:49 GMT
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
Duke - 29 Sep 2004 14:02 GMT
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]
>
>.
Tom Esh - 29 Sep 2004 18:08 GMT
>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)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.