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.
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.
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.
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
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
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
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
Assign menu bars as you are & then ALWAYS hunt things down at runtime and never use the HelpMenuItem style reference
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.
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.