The foremost Window has a Control (a Button) that opens a Modal Window. Is there a way for that Modal Window to send back a message to the foremost Window that called it (in my use case change a public Property of that foremost Window?) How would I refer to that foremost Window?
Or do I have to mediate this message through some global value that both windows would have access to.
An easy way is to have the parent window call a new Method in the Modal window which can just return the result
ModalWindow.ShowAndSetResult() as String
self.showModal // shows the modal window, stops execution until the window is closed
if self.result = "xyz" then
return "User Cancelled"
else
...
end if
The parent window then simply does:
var mw as new ModalWindow
var result as string = mw.ShowAndSetResult()
if result = "xyzzy" then
...
Typically I would have a constructor for the Sub-window that is passed the parent window.
Store this in a WeakRef in order that the parent doesnât hold a reference to the child and the child a reference to the parent, which will leak objects. With one WeakRef you can solve that problem.
In Xojo, the ShowModal method DesktopWindow â Xojo documentation is the one situation in which the rule âOnly one event at a time can be processedâ is broken. ShowModal will stop the calling code, and allow other events to happen (such as Button.action, Window.Close, etc.) and then resume program flow.
So if you really just need a simple value returned, you can use my method. If you have a lot more data to manipulate, then @Ian_Kennedy 's idea is more complicated and powerful.
Also, in Xojo Web, thereâs a different technique: You can drag an instance of a WebDialog WebDialog â Xojo documentation onto a WebPage, and then add event handlers to the Dismissed event. Since the WebDialog instance is part of the parent WebPage, you can access properties and methods of the WebPage very easily.
I donât think thereâs a similar way of handling Modal Dialogs on Desktop, is there?
Thanks. I learned something and would never have stumbled across this myself. I appreciate Ianâs contribution as well, although my needs were very simple so Mikeâs solution works for me.
I could have communicated through some global variable but I like not having to create one.
Exactly. If you created it as a local variable, it doesnât go out of scope until the method ends. Then it is truly gone. Its controls are gone, but its properties remain accessible,
Also note that after ShowModal returns, you can no longer access any of its controls, but you CAN access its properties.
That all depends upon how you close the modal window. If you use Hide, rather than Close, the windowâs control remain available for use and the progress continues in the normal way.
To be clear in the button (or what ever control triggers ending the window) do not use Self.Close but instead use âSelf.Hideâ, everything will work the way it used to but controls stay available.
Yes, it does. It is likely due to matching the behaviour of VB versions up to version 6 (after that Iâve no idea). Itâs a good trick if you donât want to add lots of code to fill properties. It does mean you break the encapsulation of the window by having control code in the calling window, which is less than desirable.