Enable and disable MenuItem

I have a Menu called Window. Inside are a few items. In particular I have Hide and Show. When the app window is visible I need Show to be disabled and Hide to be enabled. Then when hidden just the opposite

I have tried several ways of using EnableMenuItems without success.

Any suggestions?


menuHide.enabled = window.visible menuShow.enabled = not window.visible

Much depends upon how many ‘app windows’ you can have open.

  • Assuming there is only one.
  • Assuming it is called Window1

then you could put code in the Window1 enablemenuitems event, similar to Beatrix’s code above

menuHide.enabled = self.visible menuShow.enabled = not self.visible

But if ‘the’ app window is not visible, it will not update the menu, and you will not be able to show it

So this code goes into the App’s Enablemenuitems event instead

menuHide.enabled = Window1.visible menuShow.enabled = not Window1.visible

Note that the enabled state of the menu items is also controlled by the ‘Autoenable’ checkbox in the IDE.
You may want to set that to false.

If you can have several App Windows open (ie they are document windows), then you may need several Hide / Show menu items, one for each ‘App window’ in existence.

and if its on windows making things visible / invisible wont help

@Beatrix Willius - tried that but that was inconsistent with Windows OS. Then adding logic to track different kinds of visibility and hidden (I implement a timer so when user walks away this screen can timeout and l close the window requiring logging back in as a security measure) was becoming a pain. But you got me thinking

@Jeff Tullin - You got me thinking and looking further into this.

@Norman Palardy - that is a concern because I write cross platform.

So here is what I came up with. There maybe better ways but this works.
Inside the HideItem MenuHandler of Window1 I wrote this

[code]Dim c As MenuItem

c = ShowItem
c.Enabled = True

c = HideItem
c.Enabled = False

Return True[/code]

Inside ShowItem Menu Handler the opposite Boolean was used

Then in App I added those MenuHandlers as well. Inside the App level HideItem I wrote this

[code]Dim c As MenuItem

c = ShowItem
c.Enabled = True

c = HideItem
c.Enabled = False

Return True[/code]

and the opposite Boolean in ShowItem and in ShowItem I include a Window1.Visible statement as well.

This seems to work in OSX and Windows and does not require multiple MenuBar(s) and shows the user possibilities but when in the wrong state those are grayed out.

Thanks for all your input