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

Just starting out? Need help? Post your questions and find answers here.
RBart
User
User
Posts: 14
Joined: Sun May 04, 2025 11:18 am

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

Post 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
User avatar
ChrisR
Addict
Addict
Posts: 1479
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

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

Post 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
RBart
User
User
Posts: 14
Joined: Sun May 04, 2025 11:18 am

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

Post 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.
User avatar
ChrisR
Addict
Addict
Posts: 1479
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

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

Post 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
RBart
User
User
Posts: 14
Joined: Sun May 04, 2025 11:18 am

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

Post 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
User avatar
ChrisR
Addict
Addict
Posts: 1479
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

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

Post 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
RBart
User
User
Posts: 14
Joined: Sun May 04, 2025 11:18 am

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

Post 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
Post Reply