• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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
0
const71
Asked:
const71
1 Solution
 
JR2003Commented:
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now