Page 1 of 3

How to open and reopen a window from a first window?

Posted: Mon Oct 20, 2025 9:26 am
by RBart
Hi

I have a main window with a menu. And in two other files I have two other windows. I can open one or the other of the two windows. I can not open an other of the two extra windows. I can close the one that I opened but I can't reopen it nor the other anymore. Is there an easy way to do/ solve this? I guess applications with lots of windows and dynamicly build windows are not exceptional?

I am on a raspberry pi 5 with desktop raspbian

Code: Select all

; Main window
;Vensters globale variabel geven zodat gevolgd wordt of ze open of dicht zijn.
Global Venster1.i=-1 ;-1 is nog niet geopend

#ProgramTitle = "Boekhouding en facturatie"
#ProgramVersion = "v1.01"
Declare OpenSubWindow1()
Enumeration Windows
  #Main
  #Venster1
  #Venster2
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
  #MenuVenster1
  #MenuVenster2
  #MenuVenster3
  #Menuvenster4
EndEnumeration

Enumeration Gadgets
  #MainEdit
  #MainButtonOk
  #MainButtonCancel
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

;vensters
IncludeFile("venster.pb");
IncludeFile("venster2.pb")
XIncludeFile("form.pbf")
;IncludeFile("Database.pb")

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainEdit, 5, 5, dx - 10, dy - 45)
  ResizeGadget(#MainButtonok, 10, dy - 35, 120, 30)
  ResizeGadget(#MainButtonCancel, dx - 130, dy - 35, 120, 30)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
       If CreateMenu(#MainMenu, WindowID(#Main))    ; menu creation starts....
         MenuTitle("&Bestand")
          CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
            MenuItem(#PB_Menu_About, "")
          CompilerElse
            MenuItem(#MainMenuAbout, "Over")
          CompilerEndIf
          ; Menu File Items    
          CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
            MenuBar()
            MenuItem(#MainMenuExit, "E&xit")
          CompilerEndIf
        
         ;MenuTitle("&Vensters")
          ;MenuItem(#MenuVenster3, "Venster 3")
          ;MenuItem(#MenuVenster4, "Venster 4")
           ;OpenSubMenu("Recent files")       ; begin submenu
            ;MenuItem( 33, "C:\Autoexec.bat")
            ;MenuItem( 44, "D:\Test.txt")
           ;CloseSubMenu() 
          
          MenuTitle("&Vensters12")
           MenuItem(#MenuVenster1, "Venster 1")
           MenuItem(#MenuVenster2, "Venster 2")
    EndIf
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    EditorGadget(#MainEdit, 5, 5, dx -10, dy - 45)
    ButtonGadget(#MainButtonok, 10, dy - 35, 120, 30, "Ok")
    ButtonGadget(#MainButtonCancel, dx - 130, dy - 35, 120, 30, "Cancel")
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ; Event Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
              
                
              CompilerEndIf
          Case #MenuVenster1
            OpenSubWindow1() ; Roep de subvensterprocedure aan met hoofdvenster als parent 
            ;OpenWindow_0()
          Case #MenuVenster2
            OpenSubWindow2()
            
          Case #MainMenuAbout
            MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
          Case #MainMenuExit
            PostEvent(#PB_Event_CloseWindow, #Main, #Null)
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainEdit
              Select EventType()
                Case #PB_EventType_Change
                  ;
                  
              EndSelect
              
            Case #MainButtonOk            
              OpenSubWindow2();
            Case #MainButtonCancel
              ;
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
the second file with the first window to be opened

Code: Select all

; Implementatie van OpenSubWindow

;subvenster maken
Procedure OpenSubWindow1()
  If Venster1 = -1 Or IsWindow(Venster1) = 0
    Venster1 = OpenWindow(#Venster1, 150, 150, 300, 200, "Nieuw Venster", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(3, 100, 80, 100, 30, "Sluiten")
    MessageRequester("wel venster","Venster is al open",0)
  Else
    MessageRequester("geen venster","Venster nog niet open",0) 
    
  EndIf  
  
  ; Evenementen voor het subvenster
  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget And EventGadget() = 3
      CloseWindow( EventWindow())
      ;CloseWindow(#Venster1) ; Sluit het subvenster
      Venster1 = -1 ; zet terug als niet open
    EndIf
  Until EventID = #PB_Event_CloseWindow
EndProcedure
the second to be opened

Code: Select all

; Implementatie van OpenSubWindow

;subvenster maken
Procedure OpenSubWindow2()
  OpenWindow(#Venster2, 150, 150, 300, 200, "Nieuw Venster 2", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  ButtonGadget(4, 100, 80, 100, 30, "Sluiten")

  ; Evenementen voor het subvenster
  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget And EventGadget() = 4
      CloseWindow(#Venster2) ; Sluit het subvenster
    EndIf
  Until EventID = #PB_Event_CloseWindow
EndProcedure

Re: How to open and reopen a window from a first window?

Posted: Mon Oct 20, 2025 11:26 am
by ChrisR
In your 2 OpenSubWindow1(2), the window is closed when you click the button "Sluiten"
but you don't exit the event loop to return to the main event loop.

Code: Select all

  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget And EventGadget() = 4
      CloseWindow(#Venster2) ; Sluit het subvenster
    EndIf
  Until EventID = #PB_Event_CloseWindow
should be

Code: Select all

  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget And EventGadget() = 4
      CloseWindow(#Venster2) ; Sluit het subvenster
      break   ; Or EventID = #PB_Event_CloseWindow
    EndIf
  Until EventID = #PB_Event_CloseWindow

Re: How to open and reopen a window from a first window?

Posted: Mon Oct 20, 2025 12:36 pm
by RBart
ChrisR wrote: Mon Oct 20, 2025 11:26 am In your 2 OpenSubWindow1(2), the window is closed when you click the button "Sluiten"
but you don't exit the event loop to return to the main event loop.

Code: Select all

  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget And EventGadget() = 4
      CloseWindow(#Venster2) ; Sluit het subvenster
    EndIf
  Until EventID = #PB_Event_CloseWindow
should be

Code: Select all

  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget And EventGadget() = 4
      CloseWindow(#Venster2) ; Sluit het subvenster
      break   ; Or EventID = #PB_Event_CloseWindow
    EndIf
  Until EventID = #PB_Event_CloseWindow
Thank you Chris
This solved the reopenimg of the previous opened window.
If I want to open the second window from the menu. It won't open. If I close the already opened window, then it will open and close normally. I can open the windows but not have them open at the same time. Is this because the first window is still in this event loop. But then the main window is running too.

Re: How to open and reopen a window from a first window?

Posted: Mon Oct 20, 2025 12:49 pm
by ChrisR
RBart wrote: Mon Oct 20, 2025 12:36 pm If I want to open the second window from the menu. It won't open. If I close the already opened window, then it will open and close normally. I can open the windows but not have them open at the same time. Is this because the first window is still in this event loop. But then the main window is running too.
If you want to do that, you would need a single event loop.
Example for your main loop, commenting out the other two event loops in OpenSubWindow1(2)
With: do not reopen one of the two sub-windows if it is already open, just activate it

Code: Select all

Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              If IsWindow(#Venster1) : CloseWindow(#Venster1) : EndIf
              If IsWindow(#Venster2) : CloseWindow(#Venster1) : EndIf
              Break
            Case #Venster1
              CloseWindow(#Venster1)
            Case #Venster2
              CloseWindow(#Venster2)
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
              CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
              CompilerEndIf
              
            Case #MenuVenster1
              If IsWindow(#Venster1)
                SetActiveWindow(#Venster1)
              Else
                OpenSubWindow1() ; Roep de subvensterprocedure aan met hoofdvenster als parent 
              EndIf
            Case #MenuVenster2
              If IsWindow(#Venster2)
                SetActiveWindow(#Venster2)
              Else
                OpenSubWindow2()
              EndIf
              
            Case #MainMenuAbout
              MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
            Case #MainMenuExit
              PostEvent(#PB_Event_CloseWindow, #Main, #Null)
              
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventWindow()
            Case #Venster1
              If EventGadget() = 3
                CloseWindow(#Venster1)
              EndIf
            Case #Venster2
              If EventGadget() = 4
                CloseWindow(#Venster2)
              EndIf
            Case #Main
              Select EventGadget()
                Case #MainEdit
                  Select EventType()
                    Case #PB_EventType_Change
                      ;                      
                  EndSelect
                  
                Case #MainButtonOk            
                  OpenSubWindow2();
                Case #MainButtonCancel
                  If IsWindow(#Venster1) : CloseWindow(#Venster1) : EndIf
                  If IsWindow(#Venster2) : CloseWindow(#Venster2) : EndIf
                  CloseWindow(#Main)
                  Break
              EndSelect   
          EndSelect
          
      EndSelect
    ForEver

Re: How to open and reopen a window from a first window?

Posted: Mon Oct 20, 2025 2:47 pm
by RBart
ChrisR wrote: Mon Oct 20, 2025 12:49 pm
RBart wrote: Mon Oct 20, 2025 12:36 pm If I want to open the second window from the menu. It won't open. If I close the already opened window, then it will open and close normally. I can open the windows but not have them open at the same time. Is this because the first window is still in this event loop. But then the main window is running too.
If you want to do that, you would need a single event loop.
Example for your main loop, commenting out the other two event loops in OpenSubWindow1(2)
With: do not reopen one of the two sub-windows if it is already open, just activate it

Thanks again Chris,

So all the events need to be in one event loop? Or just these for navigation? If the application grows bigger, one single event loop might slow down the application and for maintenance it could become a pain over time? I could work with includes, or is there a better way?

Greetings Rudi

Re: How to open and reopen a window from a first window?

Posted: Mon Oct 20, 2025 3:22 pm
by ChrisR
Yes, If you want to navigate between multiple windows, it is better to use only 1 WaitWindowEvent(), event loop.
With multiple loop, only one loop is active at a time.
If you want to simplify reading, maintenance, you can call Sub-Procedure for each window, ex.:

Code: Select all

Procedure EventSubWindow1(EventID)
  Select EventID
    Case #PB_Event_CloseWindow
      CloseWindow(EventWindow())
    Case #PB_Event_Gadget 
      If EventGadget() = 3
        CloseWindow(EventWindow())
      EndIf
  EndSelect
EndProcedure

; Event Loop
Repeat
  EventID = WaitWindowEvent()
  ; you can also use EvtObject = EventObject(), EvtType = EventType(),... send as Procedure parameter
  Select EventWindow()
    Case #Venster1
      EventSubWindow1(EventID)
    Case #Venster2
      EventSubWindow2(EventID)
    Case #Main
      Select EventID
        Case #PB_Event_CloseWindow
          If IsWindow(#Venster1) : CloseWindow(#Venster1) : EndIf
          If IsWindow(#Venster2) : CloseWindow(#Venster1) : EndIf
          Quit = #True
          ....
      EndSelect   
  EndSelect
Until Quit

Re: How to open and reopen a window from a first window?

Posted: Mon Oct 20, 2025 4:11 pm
by RBart
Thanks Chris
I see it now.
That way the procedure (s) can be part of the file of a 'window' file. And be added to the application with a call and an include. That will keep the main event loop kind of short. And everything for each window can still be kept together in separate files. And even be reused in other applications.
Thank you again
Greetings from Belgium
Rudi

Re: How to open and reopen a window from a first window?

Posted: Mon Oct 20, 2025 6:40 pm
by TI-994A
Here's an alternative approach which implements binding functions where each window handles their events independent of the main window and can be added or omitted as required.

main_window.pb

Code: Select all

Global main_window, window2_trigger, window3_trigger

Procedure main_close_window_handler()
  CloseWindow(EventWindow())
  End
EndProcedure

main_window = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 600, 400, "Main Window", 
                         #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
window2_trigger = ButtonGadget(#PB_Any, 10, 360, 280, 30, "Open Window 2")
window3_trigger = ButtonGadget(#PB_Any, 300, 360, 280, 30, "Open Window 3")
BindEvent(#PB_Event_CloseWindow, @main_close_window_handler(), main_window)

XIncludeFile "window2.pbi"   ; add or remove as required
XIncludeFile "window3.pbi"   ; add or remove as required

Repeat : WaitWindowEvent() : ForEver

window2.pbi

Code: Select all

Declare open_window_2()

Procedure main_button1_handler()
  open_window_2()
EndProcedure

Procedure w2_button_handler()
  MessageRequester("Window 2", "Window 2 Button clicked!", #PB_MessageRequester_Ok)
EndProcedure

Procedure w2_close_window_handler()
  CloseWindow(EventWindow())
EndProcedure

Procedure open_window_2()
  window2 = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 400, 200, "Window 2", 
                       #PB_Window_SystemMenu | #PB_Window_WindowCentered, WindowID(main_window))
  w2_button = ButtonGadget(#PB_Any, 10, 160, 380, 30, "Window 2 Button")  
  BindEvent(#PB_Event_CloseWindow, @w2_close_window_handler(), window2)
  BindGadgetEvent(w2_button, @w2_button_handler())    
EndProcedure

BindGadgetEvent(window2_trigger, @main_button1_handler())

window3.pbi

Code: Select all

Declare open_window_3()

Procedure main_button2_handler()
  open_window_3()
EndProcedure

Procedure w3_button_handler()
  MessageRequester("Window 3", "Window 3 Button clicked!", #PB_MessageRequester_Ok)
EndProcedure

Procedure w3_close_window_handler()
  CloseWindow(EventWindow())
EndProcedure

Procedure open_window_3()
  window3 = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 400, 200, "Window 3", 
                       #PB_Window_SystemMenu | #PB_Window_WindowCentered, WindowID(main_window))
  w3_button = ButtonGadget(#PB_Any, 10, 160, 380, 30, "Window 3 Button")    
  BindEvent(#PB_Event_CloseWindow, @w3_close_window_handler(), window3)
  BindGadgetEvent(w3_button, @w3_button_handler())    
EndProcedure

BindGadgetEvent(window3_trigger, @main_button2_handler())

The main code will continue to work normally with or without any of the include files. :wink:

Re: How to open and reopen a window from a first window?

Posted: Mon Oct 20, 2025 9:24 pm
by ChrisR
+1 for this approach @TI-994A for multi-window :)
We can also add others BindEvent(#PB_Event_Menu, @Event_Menu_xxx...), BindEvent(#PB_Event_SizeWindow, @Resize_xxx...)
That's what I offer with the option "Bind all Gadgets Events" in IceDesign :wink:

And to be able to test each window individually, you can also add the following at the end of each windowX.pbi

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
If open_window_X()
  Repeat
    Select WaitWindowEvent()
    EndSelect
  Until Quit
EndIf
CompilerEndIf

Re: How to open and reopen a window from a first window?

Posted: Tue Oct 21, 2025 6:54 am
by RBart
@TI-994A Thank you for sharing this approach. It makes the code allot smoother. And it keeps the windows separated. Just what I was looking for.
Greetings
Rudi

Re: How to open and reopen a window from a first window?

Posted: Tue Oct 21, 2025 8:02 am
by TI-994A
ChrisR wrote: Mon Oct 20, 2025 9:24 pm+1 for this approach @TI-994A for multi-window :)
Thanks @ChrisR. And you're right; PureBasic's binding functions facilitate better modularity.

RBart wrote: Tue Oct 21, 2025 6:54 am@TI-994A Thank you for sharing this approach.
I'm glad you like it, Rudi. This is quite sufficient for most implementations, but if you require greater blackboxing of each window, perhaps you could look into PureBasic's modules. Another great feature. :D

Re: How to open and reopen a window from a first window?

Posted: Thu Oct 23, 2025 4:48 pm
by skywalk
Thanks TI-994A!
Always excellent coding guides. 8)

Aside: My only conflict is modules. I find them distracting when compared to simple prefixing.
xxx_somelibcode.pbi
xxx_somevariable, xxx_somestructure, etc.
Name conflicts are super easy to catch.
Amalgamated code is simpler for my brain.

Re: How to open and reopen a window from a first window?

Posted: Fri Oct 24, 2025 5:07 am
by TI-994A
skywalk wrote: Thu Oct 23, 2025 4:48 pm Thanks TI-994A!
Always excellent coding guides. 8)

Aside: My only conflict is modules. I find them distracting when compared to simple prefixing.
xxx_somelibcode.pbi
xxx_somevariable, xxx_somestructure, etc.
Name conflicts are super easy to catch.
Amalgamated code is simpler for my brain.
Hi @skywalk. Thank you for your kind words. :D

I share your sentiments about consolidating code into a single workspace. That's the way we've always coded!

Nevertheless, the ability to freely re-use constant, variable and procedure names across modules is quite liberating. Identical labels can be implemented across multiple modules, each containing different values and performing different functions. For example, in financial apps, there could be multiple modules with a procedure named amortise(), each containing different calculations.

Independent modules can be designed without naming constraints and namespace clashes, and without the need to hardcode prefixes to labels. Procedures are invoked simply with their module names: win1::amortise() and win2::amortise(). And even when referencing variables: win1::colour and win2::colour.

The only downside is that object numbering appears to be global, and overlaps even between modules. That's a bummer. :lol:

Re: How to open and reopen a window from a first window?

Posted: Fri Dec 26, 2025 5:06 pm
by RBart
Hi,

I wanted to use this method with the form designer but i'm getting stuck.
So I need to write the events code on another file. But how do they connect? I tryed this https://www.youtube.com/watch?v=MZyi7GjS7gM but in my case that doesn't work since I want to consistently use "BindEvents.." I need to call the procedure on the .pbf to get it open. But then I need to change the parameters. To close the form, no luck at that yet. Let alone to get any procedures working. Confusing to have the design and the events on different files.

this is the working code that works for a form written in code:

Code: Select all

Declare open_window_3(); used to tell the compiler the code is further down
                      
;================================================
;procedures to connect to main or boekhouding.pb
;================================================

Procedure main_button2_handler()
  open_window_3()
EndProcedure

BindMenuEvent(#MainMenu,#MenuWindow3,@main_button2_handler()) ;opens window3 when selected on the menu of main
;BindGadgetEvent(window3_trigger, @main_button2_handler())

;=================================================
; Window3 itself
;=================================================
Procedure w3_button_handler()
  MessageRequester("Window 3", "Window 3 Button clicked!", #PB_MessageRequester_Ok)
EndProcedure

Procedure w3_close_window_handler()
  CloseWindow(#window3)
EndProcedure

Procedure open_window_3()
  window3 = OpenWindow(#window3, #PB_Ignore, #PB_Ignore, 400, 200, "Window 3", 
                       #MainStyle, #Main); #window3 is constante gedeclareerd onder enumeratie op main.pb
  w3_button = ButtonGadget(#PB_Any, 10, 160, 380, 30, "Window 3 Button")    
  BindEvent(#PB_Event_CloseWindow, @w3_close_window_handler(), #window3)
  BindGadgetEvent(w3_button, @w3_button_handler())    
EndProcedure
this is the code of the form created with the form creator:

Code: Select all

;
; This code is automatically generated by the Form Designer.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures need to be put in another source file.
;

Global Tree_0, Panel_0

Enumeration FormWindow
  #PanelApp
EndEnumeration

Enumeration FormMenu
  #MenuItem_2
EndEnumeration


Procedure OpenPanelApp(x, y, width, height) ; needed to change this to be able to call this procedure
  OpenWindow(#PanelApp, x, y, width, height, "Boekhouding", #PB_Window_SystemMenu)
  CreateMenu(0, WindowID(#PanelApp))
  MenuTitle("Bestand")
  MenuItem(#MenuItem_2, "MenuItem2")
  MenuTitle("Bewerken")
  Tree_0 = TreeGadget(#PB_Any, 0, 0, 190, 666)
  Panel_0 = PanelGadget(#PB_Any, 190, 0, 1130, 666)
  AddGadgetItem(Panel_0, -1, "Klanten")
  AddGadgetItem(Panel_0, -1, "Offertes")
  CloseGadgetList()
EndProcedure
;BindEvent(#PB_Event_CloseWindow, @PanelApp_close_window_handler(), #PanelApp)
Procedure PanelApp_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      CloseWindow(#PanelApp) ; tryed to put the close of the form here. not working
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
        Case #MenuItem_2
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

finaly the file (.pbi) with the code that connect with the menu on main and trying to control the events on the #PanelApp form

Code: Select all

;================================================
;procedures to connect with main of boekhouding.pb
;================================================
Declare PanelApp_close_window_handler()
XIncludeFile "BoekhoudingPanelApp.pbf" ; connect the form

Procedure main_MenuPanelApp_handler() 
  OpenPanelApp(0,0,1320,710)  ; !!! altijd de parameters aanpassen na wijzigen van het ontwerp in de GUI (BoekhoudingPanelApp.pbf)!!                 
                              ; !!! Procedure OpenPanelApp(x = 0, y = 0, width = 1170, height = 610) ---> Procedure OpenPanelApp(x, y, width, height)
EndProcedure

BindMenuEvent(#MainMenu,#MenuPanelApp,@main_MenuPanelApp_handler()) ; !!Verbinding met de hoofdmenu!! van boekhouding.pb alias main. Voert de procedure "main_MenuFrmKlanten_handler()" hierboven uit.


;=================================================
; frmKlanten itself
;=================================================


Procedure PanelApp_close_window_handler()
  CloseWindow(#PanelApp)
EndProcedure
   
     
;EndProcedure
Any suggestions?

Many thanks in advance
Rudi

Re: How to open and reopen a window from a first window?

Posted: Sat Dec 27, 2025 7:06 am
by TI-994A
Here's a slightly refactored (working) version of the given code, although some wiring might be a little out of place. :wink:

BoekhoudingPanelApp.pbf (events procedure removed)

Code: Select all

;
; This code is automatically generated by the Form Designer.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures need to be put in another source file.
;

Enumeration FormWindow
  #PanelApp
EndEnumeration

Enumeration FormGadget
  #Tree_0
  #Panel_0
EndEnumeration

Enumeration FormMenu
  #MenuItem_3
  #MenuItem_4
EndEnumeration


Procedure OpenPanelApp(x = 0, y = 0, width = 0, height = 0)
  OpenWindow(#PanelApp, x, y, width, height, "Boekhouding", #PB_Window_SystemMenu | #PB_Window_ScreenCentered )
  CreateMenu(0, WindowID(#PanelApp))
  MenuTitle("Bestand")
  MenuItem(#MenuItem_3, "Open Window 3")
  MenuTitle("Bewerken")
  MenuItem(#MenuItem_4, "Close Window")
  TreeGadget(#Tree_0, 0, 0, 190, 666)
  PanelGadget(#Panel_0, 190, 0, 1130, 666)
  AddGadgetItem(#Panel_0, -1, "Klanten")
  AddGadgetItem(#Panel_0, -1, "Offertes")
  CloseGadgetList()
EndProcedure

Window3.pb

Code: Select all

Enumeration FormWindow
  #window3
EndEnumeration

Enumeration FormGadget
  #w3_button
EndEnumeration

Procedure w3_button_handler()
  MessageRequester("Window 3", "Window 3 Button clicked!", #PB_MessageRequester_Ok)
EndProcedure

Procedure w3_close_window_handler()
  CloseWindow(#window3)
EndProcedure

Procedure open_window_3()
  winFlags = #PB_Window_SystemMenu | #PB_Window_WindowCentered
  OpenWindow(#window3, #PB_Ignore, #PB_Ignore, 400, 200, "Window 3", winFlags, #PanelApp)
  ButtonGadget(#w3_button, 10, 160, 380, 30, "Window 3 Button")    
  BindEvent(#PB_Event_CloseWindow, @w3_close_window_handler(), #window3)
  BindGadgetEvent(#w3_button, @w3_button_handler())    
EndProcedure

BoekhoudingPanelApp.pb (this is the main app to run)

Code: Select all

; define and handle all events for BoekhoudingPanelApp.pbf in this code file

XIncludeFile "BoekhoudingPanelApp.pbf"
XIncludeFile "Window3.pb"

Procedure main_MenuPanelApp_menu3_handler()
  open_window_3()
EndProcedure

Procedure main_MenuPanelApp_menu4_handler()    
  PostEvent(#PB_Event_CloseWindow)
EndProcedure

Procedure openMainPanelApp() 
  OpenPanelApp(0, 0, 1000, 600)                            
EndProcedure

openMainPanelApp()

BindMenuEvent(0, #MenuItem_3, @main_MenuPanelApp_menu3_handler())
BindMenuEvent(0, #MenuItem_4, @main_MenuPanelApp_menu4_handler())

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend

Some points:
1. the recommended approach for forms is not to generate event procedures but to handle them externally instead
2. procedures do not need to be declared if they are listed higher in the code before being called
3. best practices suggest to maintain a consistent object-numbering style - either dynamically with #PB_Any or statically with constants

Hope this helps. :D