Mouse Up Event with Right-Click PopUp Menu

I want to be able to register a Right-Click Mouse Press by the user when they click within a ListBox. I want to code this in the Mouse Up event as other Windows programs do, however there seems to be a problem .... When i initiate the click (MouseDown) it is indeed within the ListBox, however when I move the mouse outside the ListBox, the MouseUp event gets called and so it brings up my Context-Sensitive Menu.

I also tried ranging within the MouseUp event like so  

LIST BOX MOUSE UP EVENT:

Private Sub TableData_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If (Button = vbRightButton) Then
        If (X >= TableData.Left And X <= TableData.Left + TableData.Width And Y >= TableData.Top) Then
            Call PopupMenu(frmWorkspace.mnuTableEntity, vbPopupMenuRightAlign)
        End If
    End If
End Sub

  but this does NOT work as I planned. The X and Y parameter dont seem to work with the LEFT, TOP WIDTH and HEIGHT.
 What is the proper way to code Right-Click PopUpMenu events for ListBox so the registered last click is exactly within the ListBox??


Thanks
const71Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
JR2003Connect With a Mentor Commented:
I think your 'If' statement is slightly wrong. Try this.

Private Sub TableData_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    With TableData
        If (Button = vbRightButton) Then
            If (X >= 0) And (X <= .Width) And (Y >= 0) And (Y <= .Height) Then
                MsgBox "hello " & X & "  " & Y
            End If
        End If
    End With

End Sub
0
 
const71Author Commented:
OK that works alot better, but when i move 1 pixel to the absolute Right and Bottom edges of the ListBox, the X and Y never match with Width and Height values respectively. They seem to be off a bit ... Why is that?

Private Sub TableData_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Debug.Print "[" & X & "] = X        [" & TableData.Width & "] = WIDTH"
    Debug.Print "[" & Y & "] = Y        [" & TableData.Height & "] = HEIGHT"

    With TableData
    If (Button = vbRightButton) Then
        If (X >= 0) And (X <= .Width) And (Y >= 0) And (Y <= .Height) Then
            Debug.Print "INSIDE"
        End If
    End If
    End With
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.