MenuItem.Enabled = False causes an IllegalCastException

I’m porting another tool to Windows and I’ve replaced the OS X Menu with a Windows Menu. However, in the code when I disable menu items, I often get an IllegalCastException when I set the specified menuitem enabled state to false. i.e.:

WinMenu.HelpMenuAbout.Enabled = False

I have double checked and the menuitem in question is named “HelpMenuAbout” and the Super is “MenuItem”.

The frustrating thing is that sometimes it occurs on other menuitems in a seemingly random fashion.

Have you tried
HelpMenuAbout.Enabled = False

Yes - that was the original code. I modified it to call the menu explicitly when I got the error hoping that I had to be explicit in Windows. Same error.

well enabled is definitely a boolean property

is the menu bar assigned to the app class & to the window ?

Yes - in App.open, I assign it at WMain’s Show

If Not QAView Then WMain.Show WMain.Menu = WinMenu Else WQuickArchive.Show WQuickArchive.Menu = WinQAMenu End If

Same code has been working under OS X for over 5 years.

Is it assigned to anything at designed time ?
But that usually leads to a different problem - the item not existing

Actually you can see that for yourself
Create a brand new desktop app
Change the App instance setting for menu bar to none
And do the same for Window1

So nothing has a menu bar assigned at design time

Then add an open event to App the reads
me.MenuBar = mainMenuBar

Then add an open event to Windows1 that says
FileQuit.Enabled = true

FileQuit will not exist

Nope - the default menus assigned in the IDE are simply called “Menu” and “QAMenu”. I change them (BTW that code snippet above is wrapped in a “#if TargetWin32” block) at run time.

Thats probably related

Actually you can see the problem with nothing assigned at design time for yourself
Create a brand new desktop app
Change the App instance setting for menu bar to none
And do the same for Window1

So nothing has a menu bar assigned at design time

Then add an open event to App the reads

me.MenuBar = mainMenuBar
Then add an open event to Windows1 that says

FileQuit.Enabled = true
FileQuit will not exist

I suspect your issue is related
Menus are quite special in this regard

Except that I’m getting an IllegalCastException instead of a NOE or a syntax error at compile time like I get if I don’t assign the menu.

Should I be assigning them to the App and the Window?

If its only attached to the Window then you can only refer to it on the window
So I’d expect a different error if your code is in the App instance

Humor me and instead of assigning them just in code assign them in the IDE itself

That worked, but it presents another problem since I need a different menu set for Windows vs. Mac

The reason I suspected that would work is that since the compiler does not know about them at compile time
So it cannot figure out what HelpAboutMenuItem is
And if it is only assigned to a Window then that named item is only available on that window

You have a limited set of options because of how menu bars work and how they even get to the compiler
We’ve run into this too and it is a pain there’s just not a decent solution to it

  1. Create one menu bar and adjust it at runtime by moving things about and removing items
    You can dynamically add them too but then you cannot refer to them by name like HelpMenuAbout

  2. Create a boolean constant - kMacMenu (or kWindowsMenu) and set it appropriately when you compile
    Then create one menu bar for Windows and one for OS X
    Then swap them manually before you compile & make darned sure your code accessing the menu items is wrapped in #if kMacMenu

  3. Assign menu bars as you are & then ALWAYS hunt things down at runtime and never use the HelpMenuItem style reference

All of these are trade offs

Tim: is it about the HelpMenuAbout only? If yes, do you want a Help -> Info menu item on Windows and a ApplicationMenu -> About menu item on OS X ? If yes, this is built-in in Xojo, have a look at AppleMenuItem.

Hi Eli,

There are many other differences between the OS X and Windows versions. The big issue here is that the assignment is changing the rules. For now, I’ve just changed the Windows menu items to turn on Autoenable and we’ll see where that takes us.