Page 1 of 1
Closing sub-windows correctly, when "Generate event procedure" is enabled (without closing entire app)?
Posted: Sun Feb 08, 2026 8:24 am
by Nudgy
I am playing around with using "Generate event procedures" for automatic event procedure generation, with two forms:
A simple program loop works fine for programs with one window:
Code: Select all
Repeat
event = WaitWindowEvent()
Until Window_1_Events(event) = #False ;Window_1 is the main form
But after the app shows the subwindow (called Window_Editor), via "OpenWindow_Editor()", closing the subwindow will end the entire program, and not just the subwindow. I am guessing that closing the subwindow is somehow triggering "Window_1_Events(event) = #False", and thereby ending the loop, but the mechanics of the event handling here are not clear to me.
What is the best way to handle the closing of a subwindow correctly, assuming that I want to use PureBasic's automatic event procedure generation?
Re: Closing sub-windows correctly, when "Generate event procedure" is enabled (without closing entire app)?
Posted: Sun Feb 08, 2026 9:53 am
by SMaag
I don't know what "Generate event procedures" is!
But I guess you try to use 2 Main Loops. 1 for the Main Window and 1 for the Child Window.
That's a problem. Only one event Loop per Application!
You have to use BindEvent or selcect your events of all windows in a single
Repeat
Until
Re: Closing sub-windows correctly, when "Generate event procedure" is enabled (without closing entire app)?
Posted: Sun Feb 08, 2026 11:53 am
by Nudgy
Thanks. I am only using one event loop.
With "Generate event procedure", I am referring to the built-in functionality in PureBasic and its form designer to create event procedures within the form file's code itself, referring to the relevant procedures, that have been manually created in the main code. If "generate event procedure" is enabled in PureBasic settings, each form will contain something like the following in its code:
Code: Select all
Procedure Window_1_Events(event)
Select event
Case #PB_Event_CloseWindow
ProcedureReturn #False
Case #PB_Event_Menu
Select EventMenu()
Case #Toolbar_MyButton
MyButton_Clicked(EventMenu())
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
EndSelect
EndSelect
ProcedureReturn #True
EndProcedure
Re: Closing sub-windows correctly, when "Generate event procedure" is enabled (without closing entire app)?
Posted: Sun Feb 08, 2026 12:02 pm
by infratec
You need
To detect which window generates the event.
Then you can use CloseWindow() to close the sub window.
Re: Closing sub-windows correctly, when "Generate event procedure" is enabled (without closing entire app)?
Posted: Sun Feb 08, 2026 4:51 pm
by Nudgy
Thanks. With this and a few additional things, it seems to work:
- In the last part of the event loop, the "Until" must include a check of which window is sending the event, e.g. "And Window = #Window_1" (although I still don't understand why "Window_1_Events(event) = #False" is fulfilled, when closing the sub-window.
- Even if the form gadgets themselves have event procedures specified via the form designer, the form itself should let the event loop handle its events, when using multiple forms.
The event loop code below works for opening sub-windows/modal windows with forms that use "generate event procedure". There's probably a more elegant solution than this, though
Code: Select all
Define WindowMainID = OpenWindow_1()
Define Window, Gadget, Menu
; Keep the window open and capture events:
Repeat
; Get the events:
Event = WaitWindowEvent()
Window = EventWindow()
Gadget = EventGadget()
Menu = EventMenu()
Select Window
; ---------- MAIN WINDOW ---------
Case #Window_1
Select Event
Case #PB_Event_CloseWindow
Debug "Window1-CloseWindow"
End
Case #PB_Event_SizeWindow
Debug "Resized Window1"
EndSelect
; ---------- SUB-WINDOW ---------
Case #Window_Editor
Select Event
Case #PB_Event_CloseWindow
Debug "Window_Editor-CloseWindow"
CloseWindow(#Window_Editor)
DisableWindow(#Window_1, #False)
Case #PB_Event_SizeWindow
Debug "Resized WindowEditor"
EndSelect
EndSelect
Until Window_1_Events(event) = #False And Window = #Window_1