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.
…but I have to admit, as a brand new XOJO user, I’m baffled as to why this simple task is so difficult.
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]
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!
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.