I have an app I’m writing that I am building for both Mac and Windows. I have a particular window that I want to have include a Close Button on the Windows version and not on the Mac version.
Seems easy enough, right? I thought I’d do something like this:
But, to my surprise, this doesn’t seem to work. It doesn’t matter if I put this code in the window’s Open event, or in it’s Constructor. Changing the CloseButton property of a window at runtime has no effect. The window will always either have (or not have) a close box based solely on the property as set at design time in the IDE. It also doesn’t matter if the target is running on OS X or Windows, or if it is compiled with Xojo 2014r3.1 or older Xojo 2013r1, all behave the same way.
I also tried setting CloseButton like this:
dim w as New Window1
w.CloseButton = true
w.Show
But, alas, this doesn’t allow changing of CloseButton at runtime either.
Is it supposed to be possible to add or remove the close box of a window at runtime?
[quote=160776:@Rob Johnston]I have an app I’m writing that I am building for both Mac and Windows. I have a particular window that I want to have include a Close Button on the Windows version and not on the Mac version.
Seems easy enough, right? I thought I’d do something like this:
But, to my surprise, this doesn’t seem to work. It doesn’t matter if I put this code in the window’s Open event, or in it’s Constructor. Changing the CloseButton property of a window at runtime has no effect. The window will always either have (or not have) a close box based solely on the property as set at design time in the IDE. It also doesn’t matter if the target is running on OS X or Windows, or if it is compiled with Xojo 2014r3.1 or older Xojo 2013r1, all behave the same way.
I also tried setting CloseButton like this:
dim w as New Window1
w.CloseButton = true
w.Show
But, alas, this doesn’t allow changing of CloseButton at runtime either.
Is it supposed to be possible to add or remove the close box of a window at runtime?
Does anyone have any ideas?[/quote]
CloseButton is read only.
The best route would be to use in fact two different windows with the same content or a Container control on it ; one with button on, the other off, and select which one to show according to the target.
The funny thing here is that I have gotten so used to right-clicking on a line of code in Xojo and having it jump directly to the help viewer for the topic (meaning that I drill directly into Window.CloseButton without looking at the parent help topic for Window), that it reveals that the documentation doesn’t say anything about “read-only” in Window.CloseButton. Yes, it is obvious when looking at the help topic for Window.
Actually, in the documentation for Window.CloseButton, it starts with:
aWindow.CloseButton = newBooleanValue
or
BooleanValue = aWindow.CloseButton
which certainly implies that it is writeable.
Oh well. Thanks for the tip. I will go with Michel’s solution, which is to have two instances of the window, one for each platform.
And to answer the “why would you do this” question – it simply has to do with how the application’s main window is used to quit the application. In this particular case, we want Mac users to use the Quit menu or hit Command-Q to Quit, where we want Windows users to click the main window’s close box.
It was my understanding that the close box and the red dot on Mac activate the same .Close event so aren’t you creating more work for yourself by having two different close functions?
If you want the app to quit when the last window is closed on Mac, add this to your App.Open event
Also, If you don’t mind a declare, in Windows, in a Window:
#if TargetWin32
Declare Function GetSystemMenu Lib "User32" ( wnd as Integer, revert as Boolean ) as Integer
Declare Sub EnableMenuItem Lib "User32" ( menu as Integer, which as Integer, flags as Integer )
dim menu as Integer
menu = GetSystemMenu( self.Handle, false )
if menu = 0 then return
Const SC_CLOSE = &hF060
Const MF_BYCOMMAND = 0
Const MF_GRAYED = 1
Const MF_ENABLED = 0
'EnableMenuItem( menu, SC_CLOSE, MF_BYCOMMAND + MF_GRAYED ) //Disable
EnableMenuItem( menu, SC_CLOSE, MF_BYCOMMAND + MF_ENABLED ) //Enable
#endif