Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

List control MouseDown or MouseUp does not return index
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



Report this thread to moderator Post Follow-up to this message
Old Post
Amarios
09-29-04 08:55 AM


Re: List control MouseDown or MouseUp does not return index
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)

Report this thread to moderator Post Follow-up to this message
Old Post
Tom Esh
09-29-04 08:55 PM


Re: List control MouseDown or MouseUp does not return index
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 
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)



Report this thread to moderator Post Follow-up to this message
Old Post
Amarios
09-30-04 09:44 AM


Re: List control MouseDown or MouseUp does not return index
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)

Report this thread to moderator Post Follow-up to this message
Old Post
Tom Esh
09-30-04 08:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Visual Basic archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:41 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.