Can't get contextual Menu to work

Hi,

I am trying to have a contextual menu (right click) to work on a canvas which should be able to move inside another canvas. I have this code in MouseDown

Blockquote
If IsContextualClick and Mire.Visible Then

ElseIf not IsContextualClick Then
myX=X
myY=Y
End if

Return True

Blockquote
in MouseDrag

Blockquote
dim i,j as Integer

i=(X-myX)
j=(Y-myY)

me.Left=me.Left+i
me.Top=me.Top+j

Blockquote
in ConstructContextualMenu

Blockquote
base.AddMenu(New DesktopMenuItem (“50x50”))
base.AddMenu(New DesktopMenuItem (“100x100”))
base.AddMenu(New DesktopMenuItem (“160x120”))
base.AddMenu(New DesktopMenuItem (“200x200”))
base.AddMenu(New DesktopMenuItem (“300x220”))
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))
base.AddMenu(New DesktopMenuItem (“Other…”))
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))
base.AddMenu(New DesktopMenuItem (“Team”))
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))
base.AddMenu(New DesktopMenuItem (“4P”))
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))
base.AddMenu(New DesktopMenuItem (“Hide Mire”))

Return True

Blockquote

And in ContextualMenuSelected

Blockquote
var t(-1) as String
var w As integer=15.75/reso
var h As Integer=19/reso
var a As Integer=4/reso

Select Case selectedItem.Text
Case “50x50”
Mire.Width=50/reso
Mire.Height=50/reso
Case “100x100”
Mire.Width=100/reso
Mire.Height=100/reso
case “160x120”
Mire.Width=160/reso
Mire.Height=120/reso
case “200x200”
Mire.Width=200/reso
Mire.Height=200/reso
case “300x220”
Mire.Width=300/reso
Mire.Height=220/reso
case “Team”

case “Other…”
MireSizeWin.ShowModal

case “4P”
Mire.Width=72/reso
Mire.Height=66/reso
case “Hide Mire”
me.Visible=False
End Select

Return True

Blockquote

I can move the canvas with a left click and drag but nothing happened with a the right click

Thanks

Chris

sorry, I think I am not using the right thing for the code :worried:

show the code

Contextual menus are meant to be built in the ConstructContextualMenu event and handled in the ContextualMenuItemSelected event.

Hello,
Christophe
my Example:

https://www.dropbox.com/scl/fi/lnsc3qoydxvh1xbyaauh5/Popup-canvas.xojo_binary_project?rlkey=c2fzyr36f9u726fob3zdevql9&dl=1

This example is using the wrong event and presents a menu for ALL clicks.

Here’s what the documentation says about why MouseDown is the wrong event to do this with:

ConstructContextualMenu(Base As DesktopMenuItem, x As Integer, y As Integer) As Boolean

This event is called when it is appropriate to display a contextual menu for the control.

This event handler is the recommended way to handle contextual menus because this event figures out whether the user has requested the contextual menu, regardless of how they did it. Depending on platform, it might be in the MouseUp or MouseDown event and it might be a right+click or by pressing the contextual menu key on the keyboard, for example.

1 Like

Hi Tim,

I am using ConstructContextualMenu

base.AddMenu(New DesktopMenuItem ("50x50"))
base.AddMenu(New DesktopMenuItem ("100x100"))
base.AddMenu(New DesktopMenuItem ("160x120"))
base.AddMenu(New DesktopMenuItem ("200x200"))
base.AddMenu(New DesktopMenuItem ("300x220"))
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))
base.AddMenu(New DesktopMenuItem ("Other..."))
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))
base.AddMenu(New DesktopMenuItem ("Team"))
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))
base.AddMenu(New DesktopMenuItem ("4P"))
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))
base.AddMenu(New DesktopMenuItem ("Hide Mire"))

Return True

and ContextualMenuSelected

var t(-1) as String
var w As integer=15.75/reso
var h As Integer=19/reso
var a As Integer=4/reso


Select Case selectedItem.Text
Case "50x50"
  Mire.Width=50/reso
  Mire.Height=50/reso
Case "100x100"
  Mire.Width=100/reso
  Mire.Height=100/reso
case "160x120"
  Mire.Width=160/reso
  Mire.Height=120/reso
case "200x200"
  Mire.Width=200/reso
  Mire.Height=200/reso
case "300x220"
  Mire.Width=300/reso
  Mire.Height=220/reso
case "Team"
  
case "Other..." 
  MireSizeWin.ShowModal
  
case "4P"
  Mire.Width=72/reso
  Mire.Height=66/reso
case "Hide Mire"
  me.Visible=False
End Select

Return True

The problem is the code in MouseDown

If IsContextualClick and Mire.Visible Then
  
  
ElseIf not IsContextualClick Then
  myX=X
  myY=Y
End if

Return True

If I use this code, I can move the canvas around but when I right clicked nothing is happening
If I don’t put any code I can use the right click and the contextual menu occurred …

Thanks

By the way Rudolf, thanks a lot for your exemple, I still consider to use it if I can’t find the “right” solution with Tim’s help…

You need to respond to MouseDown with false to indicate to the framework that you did not handle the mouse event for the contextual click. Returning true tells the framework that you handled the mouse event and that it should not. This is detailed in the documentation for the MouseDown event.

The easiest way would be to return early, adding a bunch of ifs with nesting can get really cumbersome. At the top of MouseDown add this:

if IsContextualClick then return false

works in both versions
new

https://www.dropbox.com/scl/fi/bd9y7sob8bszhhzpe444v/Popup-canvas-2.xojo_binary_project?rlkey=fr8w8ahw1j9iwk8x4ow2jqv7t&dl=1

Right, but there’s something to be said about giving people an arguably wrong answer immediately after someone posts the correct events with links to the documentation.

Returning true tells the framework that you handled the mouse event and that it should not

Read: “Returning true tells the framework that you handled the mouse event,
and that the framework itself should not handle.”

Great :smile:
Works perfect and the “right” way Tim
Thanks a lot to the 3 of you specially for being so quick to help me out
Chris

Use </> for code.

Thanks Tim I ended up finding the right </> for code :smile:

Mark it as the solution, by clicking ‘solution’ at the bottom of the post you used ,please. Otherwise the post shows up as ‘in progress/ unsolved’ forever. :slight_smile:

Hi Jeff,

Sorry I will from now on

You can still do it for this thread :wink:

Sorry again, where or how should I do this?

Click this under the post you used.

image