Contextual menu when clicking on a DesktopRectangle

I have the following code that correctly catches a Double Click on a rectangle inside a canvas, inside the canvas’s DoublePressed event:

//Check all rectangles on this canvas ( in mRects() ) 
//to see if we double clicked on one 
For i As Integer = mRects.LastIndex DownTo 0
  Var r As BITCObject = mRects(i)
  If r.ClickedWithin(x, y) Then
    mClickedRect = r
    mRectIndex = i
    Var w As New SettingsWindow
    w.ShowPopover(me)
    r.isSelected = true
  End If
Next

This works perfectly.

I used the same code inside the ConstructContextualMenu event for the canvas, and a right click on a rectangle in the canvas never fires:

For i As Integer = mRects.LastIndex DownTo 0
  Var r As BITCObject = mRects(i)
  If r.ClickedWithin(x, y) Then
    mClickedRect = r
    
    // Build context menu
    base.AddMenu(New MenuItem("Delete"))
    base.AddMenu(New MenuItem("Change Color"))
    base.AddMenu(New MenuItem("Duplicate"))
    Return True
  end if
next

If I put a break at the top of the ConstructContextualMenu event, it’s never triggered if I right click on a rectangle. But it is triggered if I click on the canvas outside a rectangle. Since the click is happening within the canvas (even though it’s on the rectangle within that canvas), and the ConstructContextualMenu event is for the canvas, shouldn’t this event get triggered like DoublePressed does?

So I’m really confused here. I made a test project with a canvas and a drawn rectangle. when I right click on either the background of the canvas or the rectangle on the canvas, I can get a contextual menu to appear.

But in my app I can’t even get ConstructContextualMenu to fire when I right click in a rectangle. Any ideas?

Ok, I figured it out. The problem was that the MouseDown event in the Canvas was overriding ConstructContextualMenu. By checking to see that isContextualClick = false before handling the mousedown event, the code in ConstructContextualMenu event gets fired. That is, this, in MouseDown for the canvas:

if IsContextualClick = false then
  For i As Integer = mRects.LastIndex DownTo 0
    Var r As BITCObject = mRects(i)
    If r.ClickedWithin(x, y) Then
      mDragRect = r
      mOffsetX = x - r.X
      mOffsetY = y - r.Y
      r.isSelected = true
      Return True
    End If
  Next
end if
1 Like