For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > September 2004 > List control MouseDown or MouseUp does not return index









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author List control MouseDown or MouseUp does not return index
Amarios

2004-09-29, 3:55 am

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


Tom Esh

2004-09-29, 3:55 pm

On 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)
Amarios

2004-09-30, 4:44 am

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


"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[color=darkred]
by[color=darkred]
>
> 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)



Tom Esh

2004-09-30, 3:55 pm

On 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)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com