add new item to main menu

I suspect this is a problem of where the code is placed.

I need to add a menu item to the “Utility” menu of my apps main menu based on who logs in. If it’s a programmer (that means me…) I want a menu item to show that only I can select. Don’t even want a user to know it’s there. I’m sure there are other ways to set this up but I’m also trying to learn how to manipulate the main menu.

The item gets added ok, but it is not enabled. I added the AutoEnable line and it is still not enabled. I did read where the enable method can only be called from within the EnableMenu Event Handler. But I don’t want the item added until after the app has started and the user has logged in.

thanks for any help…

dim miSpecialAccess As New MenuItem
miSpecialAccess.Text = "Programmer Access"
miSpecialAccess.AutoEnable = True 
UtilityMenu.Append(miSpecialAccess)

if you dont have any menu handlers defined for this item then it will never autoenable

Add a menu at design time
Add a handler.
At run time, if the user is not a developer, set the caption to be “-” and it turns into a separator.

try this

  1. create a new desktop app
  2. add the “UtilityMenu” the the main menu bar (and add whatever items you want - 1 or 2 just so its not empty will be fine - this IS just a demo and we could add and remove the entire utility menu in much the same way)
  3. in the Window1.open event add the following code
dim miSpecialAccess As New MenuItem
miSpecialAccess.Name = "programmerMode"
miSpecialAccess.Text = "Programmer Access"
miSpecialAccess.AutoEnable = True 
UtilityMenu.Append(miSpecialAccess)
  1. in Window1 add a menu handler “programmerMode” (note we use the NAME of the item not the text etc)
    just put a break in the menu handler itself (or a break point on the return statement - either will work)
  2. add a new window - Window2
  3. in app.open add “dim w as new Window2”
  4. run

you may want to change the titles on window1 and window2 so you can tell which window is which

and when you switch between window1 and window2 note what happens to the menu

autoenable literally is “if there’s a defined menu handler then enable otherwise dont”
and you can add menu handlers even if the menu item doesn’t exist yet
BUT note that the NAME is the name of the menu handler etc so definitely set one

The proposed solutions sound rather complicated.

Add the menu at design time.
Forget about the autoenable.
In the enableMenuItems event do something like

if user = "developer" then secretMenu.enabled = true else secretMenu.close end if

if you add it IN the design time then its on the menu bar from the outset and you have to decide before someone has logged in whether or not it should be there - exactly what william does not want

ie/ open Xojo and get to the “select a project” dialog and then peruse the menu bar
the menus will all show up and you can see all the sub items etc but they wont be enabled

IF this item we’re there in design time users could see it at that point

adding it dynamically works just fine and does exactly what william wanted