Creating a dynamic menu issue

I am creating a dynamic menu with sub items, it creates fine but all the sub items are disabled.

Dim SelfMenu As New MenuItem(“Self Service”)
Main_MenuBar.Insert(2, SelfMenu)
SelfMenu.Enabled = True

Dim fMenu As MenuItem

fMenu = New MenuItem(“selfchgpswd”)
fMenu.Text = “Change Password”
fMenu.Enabled = True
SelfMenu.Append(fMenu)

fMenu = New MenuItem(“selfeditprofile”)
fMenu.Text = “Edit Profile”
fMenu.Enabled = True
SelfMenu.Append(fMenu)

fMenu = New MenuItem(“selfreqrole”)
fMenu.Text = “Request Role”
fMenu.Enabled = True
SelfMenu.Append(fMenu)

fMenu = New MenuItem(“selfuserdir”)
fMenu.Text = “User Directory”
fMenu.Enabled = True
SelfMenu.Append(fMenu)

Did you subClass a MenuItem and use that to populate a Menu ?

If No, do it.

In the subClass, you ill get the MenuItem # and this allows you to get full functional MenuItems.

This event of the menubar will probably help:
http://documentation.xojo.com/api/deprecated/menuitem.html#menuitem-enableMenu

I supposed you mean:
“In a designed with the Menu Editor screen, I created a Menu in the MenuBar that I populated dynamically by code (elsewhere, probably in the App.Open Event)…”

Read the last entry there: http://documentation.xojo.com/api/language/me.htmlnuItem (below Dynamic Menus Code Samples)

I have created a subclass of menuitem called SelfMenuItem.

I am running on windows.

I have tried calling it in both the MainWindow Open and EnableMenuItems events.

The items are created but they are all disabled.

Dim SelfMenu As New SelfMenuItem(“Self Service”)
Main_MenuBar.Insert(2, SelfMenu)
SelfMenu.Enabled = True
SelfMenu.AutoEnable = True

Dim fMenu As SelfMenuItem

fMenu = New SelfMenuItem(“selfchgpswd”)
fMenu.Text = “Change Password”
fMenu.Enabled = True
fMenu.AutoEnable = True
SelfMenu.Append(fMenu)

fMenu = New SelfMenuItem(“selfeditprofile”)
fMenu.Text = “Edit Profile”
fMenu.Enabled = True
fMenu.AutoEnable = True
SelfMenu.Append(fMenu)

fMenu = New SelfMenuItem(“selfreqrole”)
fMenu.Text = “Request Role”
fMenu.Enabled = True
fMenu.AutoEnable = True
SelfMenu.Append(fMenu)

fMenu = New SelfMenuItem(“selfuserdir”)
fMenu.Text = “User Directory”
fMenu.Enabled = True
fMenu.AutoEnable = True
SelfMenu.Append(fMenu)

they require a menuhandler to become “active” … as is they would do “nothing” even if you could click them

[quote=442898:@LANCE DARBY]I am creating a dynamic menu with sub items, it creates fine but all the sub items are disabled.

Dim SelfMenu As New MenuItem(“Self Service”)
Main_MenuBar.Insert(2, SelfMenu)
SelfMenu.Enabled = True

Dim fMenu As MenuItem

fMenu = New MenuItem(“selfchgpswd”)
fMenu.Text = “Change Password”
fMenu.Enabled = True
SelfMenu.Append(fMenu)

fMenu = New MenuItem(“selfeditprofile”)
fMenu.Text = “Edit Profile”
fMenu.Enabled = True
SelfMenu.Append(fMenu)

fMenu = New MenuItem(“selfreqrole”)
fMenu.Text = “Request Role”
fMenu.Enabled = True
SelfMenu.Append(fMenu)

fMenu = New MenuItem(“selfuserdir”)
fMenu.Text = “User Directory”
fMenu.Enabled = True
SelfMenu.Append(fMenu)[/quote]

Note this doesnt REQUIRE using a subclass at all … thats one way to handle this

Have you defined menu handlers for any of the sub items ?
The fun part is that

  1. you MUST assign the NAME - not just the text
  2. you CAN define a menu handler even for items that dont already exist in the menu bar (you just have to put the names into the editor field when you create one)
  3. once you do this the items will automatically enable & disable on windows where those menu handlers are defined

STEP 1 : alter your code - NOTE THE ADDITION OF THE SETTING OF THE NAME PROPERTY !

Dim SelfMenu As New MenuItem("Self Service")
Main_MenuBar.Insert(2, SelfMenu)
SelfMenu.Enabled = True

Dim fMenu As MenuItem

fMenu = New MenuItem("selfchgpswd")
fMenu.Name = "selfchgpswd" // <<<<<<<< THIS IS VITAL
fMenu.Text = "Change Password"
SelfMenu.Append(fMenu)

STEP 2 : ADD A NEW MENU HANDLER USING THE NAME

so you should now have a menu handler for “selfchgpswd”

STEP 3 : add your code into the menu handler

Here’s a sample done this way

A blog post about how you can do this
https://www.great-white-software.com/blog/2019/06/28/dynamic-menu-handling/