Enable/Disable MenuItems

I’m having trouble simply enabling and disabling menu items (XOJO Version 2016 Release 3).

I start a new project (Windows App). Add a generic button, and in its “Action” event handler enter:

EditMenu.Enabled = False FileQuit.Enabled = False

I run the app, and press the button, but nothing happens. Both the “Exit” (FileQuit) and Edit (EditMenu) menus are still enabled.


You can only do that from within an EnableMenuItems event. Won’t work from anywhere else.



EditMenu.Visible = False FileQuit.Visible = False

Thank you both for your prompt replies!

It seems very odd that the “Visible” property can be updated easily but the “Enabled” property requires a dedicated event?

Unfortunately, I’m still not clear how this simple functionality (a button that toggles whether a menu item is enabled/disabled) is achieved.

The EnableMenuItems documentation shows there are no parameters, so in my button’s action event handler, I have:

If FileQuit.Enabled Then FileQuit.Enabled = False Else FileQuit.Enabled = True End If EnableMenuItems

But this doesn’t seem to work either… :frowning:

Grant: Can you elaborate on how I would achieve this simple functionality using the EnableMenuItems event, as you’ve suggested?

EnableMenuItems is an event, not a method.

Add the event to the app and/or a document window

In the event, add
FileQuit.Enabled = False

(Im not convinced this is a menu item you SHOULD disable, but you know your own thinking…)

These need to be inside the EnableMenuItems event

If FileQuit.Enabled Then
    FileQuit.Enabled = False
    FileQuit.Enabled = True
  End If

this event is called just prior to the menu opening

it works for controls

say you have a TextArea then and in


If FileQuit.Enabled Then FileQuit.Enabled = False Else FileQuit.Enabled = True End If

When TextArea has Focus then FileQuit is disabled

You can not do this in a Button.

Dave was faster.

From the User Guide:

You can also manually control when menu items are enabled. To do this, its AutoEnable property must be False (or OFF). In this case, the menu item will always appear as disabled. In order to enable it, you have to add code to the EnableMenuItems event handler for the containing Window. For example, you might have code that enables a Save menu item only when the document has been changed:

If Self.ContentsChanged Then SaveMenu.Enable End If
To enable a menu item, you can call its Enable method or set its Enabled property to True.

Every time the menu is displayed, all the menuitems are reset and the EnableMenuItems events are called on the current control, window and app. So you can’t change the Enabled status directly. It’s just going to be reset when the menu is opened. What you should do is set a flag/flags that can be used in the EnableMenuItems event to control the status of the menu.

Thank you all for your input. Much appreciated.

…but I have to admit, as a brand new XOJO user, I’m baffled as to why this simple task is so difficult. :frowning:
It makes no sense why “FileQuit.Enabled” cannot be toggled within a button action event, but “FileQuit.Visible” can be intuitively toggled as you’d expect (“FileQuit.Visible = False”)…

Dave S: Your suggestion does not appear to work. It just permanently disables the menuitem

Axel Schneider: So what you’re saying is that it is not possible to disable a menu-item with a button?

Tim Hare: I’ll have to try it out. Seems like a plausible workaround…

it is quite simple… any changes in the ENABLE property of a menuitem is done INSIDE the EnableMenuItems event. period
Not sure how much more simple it can be… perhaps not what you want to be. but it is what it is.

And my suggestion works… its just that you put the wrong code there… look at it, think about it, and you will see why it “doesn’t work” (hint… you can’t toggle ENABLE as explained before, they are RESET automatically, therefore they will NEVER toggle the way you thought they would)

simply put. [quote]The ENABLE property of a MENUITEM has a NON-PERSISTANT value[/quote]

Ok, it appears Tim Hare’s work-around works…

I created a boolean Window property: “FileQuitEnabledFlag”, initiated to true in the Window’s Open event handler.

In the button’s “Action” event handler I put:

If FileQuitEnabledFlag Then FileQuitEnabledFlag = False Else FileQuitEnabledFlag = True End If EnableMenuItems

And finally, in the Window’s “EnableMenuItems” event handler:

If FileQuitEnabledFlag Then FileQuit.Enabled = True Else FileQuit.Enabled = False End If

Whew! Like I say, I’m brand new to XOJO, and this was the very first thing I tried to do in order get a sense of XOJO’s workflow: a simple task of using a button to toggle a property.
I’m guessing it was purely bad luck that I chose a property that isn’t so simple/intuitive to set… I’ll keep tinkering with XOJO and see, I guess…

Even though this exercise was, unfortunately, not the best first experience using XOJO, one thing can be said for certain: XOJO has a very helpful, responsive community! Thank you all for the introductory lesson and feedback! :slight_smile:

in EnableMenuItems just put

    FileQuit.Enabled = FileQuitEnabledFlag

and thats not a “workaround”, a workaround is a band-aid to fix something that is broken, this isn’t broken


Been busy lately but I was going to suggest Tim’s example, however, using a boolean property in an if…then test to set a boolean value is a little inefficient (sorry Tim). Dave has the right idea, and I would go further and change your button’s action event and replace it all with just one line of code:

FileQuitEnabledFlag = not FileQuitEnabledFlag

This will toggle the value of the FileQuitEnabledFlag property from true to false, and from false to true each time you click your button.


Huh? I didn’t post any code.

Ooops! That should’ve said sorry Mike. Sorry Tim.