Custom Popup menu outside of contructcontextualmenu

I use a rich text editor control that I developed myself. I need to create a popup menu shown when the user right clicks the control. The right click event is raised via MouseClickRight after which the code below executes and a popupmenu is displayed with dynamically created items only. There is no ContextualMenuSelected event used so I get the selected menu with the code below. It works, but feels sluggish and I’m getting weird / random crashes from time to time. I’ve simplified the code below… what is wrong with this?

Furthermore, is there no way to use the Tag property of the selected menu. Using the approach below I read in the documentation that only the Text property can be used??

Var popMenu, m As DesktopMenuItem

// Add a menu item to our popup menu
m = New DesktopMenuItem("My custom menu")
m.Tag = "MyTag"

// Show a popup menu
var mSelected as DesktopMenuItem
mSelected = popMenu.PopUp(system.MouseX, system.MouseY)

// Handle the selected menu option
if s <> nil then
  if s.Text = "SelectedMenuTextvalue" then
    // Success code

  end if
end if

Why can’t you use the Tag? What is “sluggish”? What crashes do you get? This is just a popupmenu. I have used this type of popupmenu for years. There was no sluggishness and for sure there were no crashes.

Which OS do you use and which version of Xojo.

Latest version of Xojo, MacOS (latest) on a MacBook Pro 2023.

So it is valid to call the Popup function, and assigning the return value to a new desktopmenuitem like I do above?

There’s nothing conceptually wrong with your code. I do notice that you never initialize the “popup” variable, it should throw a NilObjectException when you try to add the new menu item to it.

Yeah, that’s just a typo in the demo code, but fixed. Glad to hear this is the right approach, but it seems to behave differently than a popupmenu generated within the contextual event provided by xojo, slower in opening submenus etc. (when hovering) and also like mentioned before, causes a crash from time to time.

Show us the crash log.

your demo code looks odd. you get the menu result in mSelected and later you test s <> nil

Var menu As New DesktopMenuItem
menu.AddMenu(New DesktopMenuItem("Text","Tag"))
Var mSelected As DesktopMenuItem = menu.PopUp(System.MouseX, System.MouseY)
If mSelected<>Nil Then
  Select Case mSelected.Tag
  Case "Tag"
    System.DebugLog mSelected.Tag
  End Select
End If