4

How can I disable a NSMenuItem? I try to disable a NSMenuItem after it has been clicked. The click is handled correctly by the Action (Logout).

I tried changing the Enabled property to false in the following two ways:

partial void Logout (AppKit.NSMenuItem sender)
{
   sender.Enabled = false;
}

and

partial void Logout (AppKit.NSMenuItem sender)
{
   LogoutItemOutlet.Enabled = false;
}

But every time this action is called, the Enabled fields keep being true. The menu item itself also never actually disables.

How to disable the NSMenuItem after an action is executed?

Waaghals
  • 2,029
  • 16
  • 30

2 Answers2

4

Try setting menu item's Action to null:

partial void Logout (AppKit.NSMenuItem sender)
{
   LogoutItemOutlet.Action = null;

   // I don't think you need the following but it won't hurt
   LogoutItemOutlet.Enabled = false;
}
ashokgelal
  • 80,002
  • 26
  • 71
  • 84
  • Thanks, this does work. I'm re-enabling the butting with `ObjCRuntime.Selector("NameOfAction:")`. Do you know if this is actually the correct way? – Waaghals Feb 19 '16 at 13:02
  • I'm not sure if it's the correct way or not but an alternative way could be to subscribe/unsubscribe from Activated event: To disable `LogoutItemOutlet.Activated -= LogoutActivatedHandler;` and to enable `LogoutItemOutlet.Activated += LogoutActivatedHandler;` – ashokgelal Feb 21 '16 at 17:33
0

NSMenuItem Enabled property has no effect unless the NSMenu in which the item will be added or is already a part of has AutoEnablesItems set to true (it is false by default). This property can be set programmatically, or in Interface Builder.

c0lby
  • 226
  • 2
  • 3
  • The setting in the Interface Builder is already enabled, also when debugging the `Menu` property of my NSStatusItem is already set to true. – Waaghals Feb 18 '16 at 15:51