I use ConstructContextualMenu event on a Canvas to build a menu, items depend on mouse location and therefore different menus are built.
Code is like:
if Pos = a then
base.append(new menuitem("Pos A item")
elseif Pos = b then
base.append(new menuitem("Pos B item")
else
return false
end if
return True
They are some places on the Canvas were no menu should be displayed, on Windows I just don’t append items to base and it works: no contextual menu is displayed; but on Mac it always displays the last build menu even if I return False (it seems returning True or False doesn’t matter).
It’s like a menu items “cache” for each control. Is it possible to empty the menu ?
[quote=128570:@Alejandro Fresno Meyer]<…> on Mac it always displays the last build menu even if I return False (it seems returning True or False doesn’t matter).
It’s like a menu items “cache” for each control. Is it possible to empty the menu ?[/quote]
Yes, I stumbled over that one, too. So I wrote a method to clear the menus before rebuilding them.
In your case you could sublass canvas and add a property of type MenuItem to it, let’s name it mBase. Then you could add such method to your subclass:
Sub ContextMenuClear()
// Clear Contextual Menu
If Me.mBase <> Nil Then
Dim j As Integer = Me.mBase.Count-1
For i As Integer = j DownTo 0
Me.mBase.Remove(i)
Next
Me.mBase = Nil
End If
End Sub
Now you can do this:
myCanvas.ContextMenuClear
Oh yes, and in the ConstructContextualMenu handler you then can set:
Me.mBase = base
and then append your context menuitems to Me.mBase
Oliver, thanks for your answer.
Unfortunatelly I can’t get it working.
mBase is correctly populated with the items and also removed at ContextMenuClear but still the last menu items appear when no menu should be available.
May be there is some OS restriction and shows a cached contextual menu for the same control. Will try to build my own menu at MouseDown.
[quote=128579:@Alejandro Fresno Meyer]Oliver, thanks for your answer.
Unfortunatelly I can’t get it working.
mBase is correctly populated with the items and also removed at ContextMenuClear but still the last menu items appear when no menu should be available.
May be there is some OS restriction and shows a cached contextual menu for the same control. Will try to build my own menu at MouseDown.[/quote]
You could try this in ConstructContextualMenu to reset base :
base = New MenuItem
I should prevent the caching.
Thanks Michel, I tried this solution but then no menu is displayed.
I solved the issue by creating and displaying the menus at the MouseDown Event the old way instead of using ContextualMenu events:
If IsContextualClick then
if PosA then
Dim MyMenu as new MenuItem
MyMenu.Append(new menuitem("Menu1"))
Dim MyHitItem as MenuItem
MyHitItem = MyMenu.PopUp
if MyHitItem <> nil then
'do action
end if
else
'no menu
end if
end if
This still works for Mac and for Windows
Its a bug - There is a feedback case for it
Try out https://github.com/CharlieXojo/classContextualMenu