Page 1 of 1

How to set window close event to match each window?

Posted: Tue Oct 21, 2025 1:03 pm
by hdt888
I have 3 windows: exit, welcome and main.

Nothing happened until I tried put the #PB_Event_CloseWindow event for each Windows. It didn't work as expected.

How to set window close event to match each window?

That means I want to separate the #PB_Event_CloseWindow event into each window's own module. Not put it in the "main.pbi" module.

And so, the "main.pbi" module is left with just the following simple line of code:

Code: Select all

Repeat : WaitWindowEvent() : ForEver
1. declare_program.pbi

Code: Select all

DeclareModule declare_program
  
  Enumeration windows
  EndEnumeration
  
  Enumeration gadgets
  EndEnumeration
  
  Structure my_data
    welcome.i
  EndStructure
  
  Global.my_data *mydata = AllocateStructure(my_data)
  
EndDeclareModule

Module declare_program
  *mydata\welcome = #False
EndModule

DeclareModule exit_window
  UseModule declare_program
  
  Enumeration windows
    #ui_exit_window
  EndEnumeration
  
  Enumeration gadgets
    #exit_btn_yes
    #exit_btn_no
  EndEnumeration
  
  Declare open_exit_window()
  
EndDeclareModule

DeclareModule welcome_window
  UseModule declare_program
  
  Enumeration windows
    #ui_welcome_window
  EndEnumeration
  
  Enumeration gadgets
    #welcome_btn_start
  EndEnumeration
  
  Declare open_welcome_window()
  
EndDeclareModule

DeclareModule main_window
  UseModule declare_program
  
  Enumeration windows
    #ui_main_window
  EndEnumeration
  
  Enumeration gadgets
    #main_btn_1
    #main_btn_2
  EndEnumeration
  
  Declare open_main_window()
  
EndDeclareModule
2. main.pbi (main to build/run)

Code: Select all

XIncludeFile "declare_program.pbi"
XIncludeFile "ui_exit.pbi"
XIncludeFile "ui_welcome.pbi"
XIncludeFile "ui_main.pbi"

UseModule declare_program
UseModule exit_window
UseModule welcome_window
UseModule main_window

If *mydata\welcome = #True
  open_welcome_window()
Else
  open_main_window()
EndIf

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Select EventWindow()
        Case #ui_welcome_window
          DisableWindow(#ui_welcome_window, #True)
          open_exit_window()
        Case #ui_main_window
          DisableWindow(#ui_main_window, #True)
          open_exit_window()
        Case #ui_exit_window
          If IsWindow(#ui_welcome_window)
            DisableWindow(#ui_welcome_window, #False)
          EndIf
          If IsWindow(#ui_main_window)
            DisableWindow(#ui_main_window, #False)
          EndIf
          CloseWindow(#ui_exit_window)
      EndSelect 
  EndSelect
ForEver
3. ui_exit.pbi

Code: Select all

Module exit_window
  
  UseModule welcome_window
  UseModule main_window
  
  Procedure events_exit_window()
    Select Event()
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #exit_btn_no : PostEvent(#PB_Event_CloseWindow, #ui_exit_window, #PB_Ignore)
          Case #exit_btn_yes : End
        EndSelect
    EndSelect
  EndProcedure
  
  Procedure open_exit_window()
    OpenWindow(#ui_exit_window, #PB_Ignore, #PB_Ignore, 190, 100, "Exit ?", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(#exit_btn_no, 10, 50, 80, 45, "No")
    ButtonGadget(#exit_btn_yes, 100, 50, 80, 45, "Yes")
    BindEvent(#PB_Event_Menu, @events_exit_window())
    BindEvent(#PB_Event_Gadget, @events_exit_window())
  EndProcedure
EndModule
4. ui_welcome.pbi

Code: Select all

Module welcome_window
  
  UseModule exit_window
  UseModule main_window
  
  Procedure welcome_btn_start_handler()
    HideWindow(#ui_welcome_window, #True)
    open_main_window()
  EndProcedure
  
  Procedure events_welcome_window()
    Select Event()
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #welcome_btn_start : CloseWindow(#ui_welcome_window) : open_main_window()
          EndSelect
      EndSelect
  EndProcedure
  
  Procedure open_welcome_window()
    OpenWindow(#ui_welcome_window, #PB_Ignore, #PB_Ignore, 300, 400, "Welcome Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(#welcome_btn_start, 10, 360, 280, 30, "Start")
    BindEvent(#PB_Event_Menu, @events_welcome_window())
    BindEvent(#PB_Event_Gadget, @events_welcome_window())
  EndProcedure

EndModule
5. ui_main.pbi

Code: Select all

Module main_window
  
  UseModule exit_window
  
  Procedure events_main_window()
    Select Event()
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #main_btn_1 : Debug "#main_btn_1"
            Case #main_btn_2 : Debug "#main_btn_2"
          EndSelect
      EndSelect
  EndProcedure
  
  Procedure open_main_window()
    OpenWindow(#ui_main_window, #PB_Ignore, #PB_Ignore, 600, 400, "Main Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(#main_btn_1, 10, 360, 280, 30, "Button 1")
    ButtonGadget(#main_btn_2, 300, 360, 280, 30, "Button 2")
    BindEvent(#PB_Event_Menu, @events_main_window())
    BindEvent(#PB_Event_Gadget, @events_main_window())
  EndProcedure
EndModule
Thanks for support.

Re: How to set window close event to match each window?

Posted: Tue Oct 21, 2025 1:19 pm
by STARGÅTE
You can bind the event on each window separately:

Code: Select all

BindEvent(#PB_Event_CloseWindow, @Callback(), #Window) 

Re: How to set window close event to match each window?

Posted: Tue Oct 21, 2025 1:27 pm
by ChrisR
Yes, I was writing...
I don't have time to test it right now. but just by looking at the shared codes

Code: Select all

BindEvent(#PB_Event_Menu, @events_main_window())
BindEvent(#PB_Event_Gadget, @events_main_window())
Is used multiple times, with different procedures and without specifying the window
it would be better with:

Code: Select all

BindGadgetEvent(#main_btn_1, @event_main_btn_1())
BindGadgetEvent(#main_btn_2, @event_main_btn_2())
BindEvent(#PB_Event_Menu, @event_menu_main_window(), #ui_main_window)
BindEvent(#PB_Event_CloseWindow, @Event_close_main_window(), #ui_main_window)
with the event procedures for each window, Gadget

Code: Select all

Procedure Event_close_main_window()
  Quit = #True
EndProcedure
To facilitate testing, you could concatenate the different *.pbi files into a single one, just copy the code in place of XIncludeFile

Re: How to set window close event to match each window?

Posted: Tue Oct 21, 2025 1:40 pm
by TI-994A
Like @STARGÅTE and @ChrisR had said, bind the close window events to each window expressly. Here's the integrated, working example of your code with these amendments:

Code: Select all

; declare_program.pbi

DeclareModule declare_program
  
  Enumeration windows
  EndEnumeration
  
  Enumeration gadgets
  EndEnumeration
  
  Structure my_data
    welcome.i
  EndStructure
  
  Global.my_data *mydata = AllocateStructure(my_data)
  
EndDeclareModule

Module declare_program
  *mydata\welcome = #True
EndModule

DeclareModule exit_window
  UseModule declare_program
  
  Enumeration windows
    #ui_exit_window
  EndEnumeration
  
  Enumeration gadgets
    #exit_btn_yes
    #exit_btn_no
  EndEnumeration
  
  Declare open_exit_window()
  
EndDeclareModule

DeclareModule welcome_window
  UseModule declare_program
  
  Enumeration windows
    #ui_welcome_window
  EndEnumeration
  
  Enumeration gadgets
    #welcome_btn_start
  EndEnumeration
  
  Declare open_welcome_window()
  
EndDeclareModule

DeclareModule main_window
  UseModule declare_program
  
  Enumeration windows
    #ui_main_window
  EndEnumeration
  
  Enumeration gadgets
    #main_btn_1
    #main_btn_2
  EndEnumeration
  
  Declare open_main_window()
  
EndDeclareModule



; ui_exit.pbi

Module exit_window
  
  UseModule welcome_window
  UseModule main_window
    
  Procedure events_exit_window()
    Select Event()
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #exit_btn_no : PostEvent(#PB_Event_CloseWindow, #ui_exit_window, #PB_Ignore)
          Case #exit_btn_yes : End
        EndSelect
      Case #PB_Event_CloseWindow
        CloseWindow(#ui_exit_window)
        If IsWindow(#ui_welcome_window)
          DisableWindow(#ui_welcome_window, #False)
        Else
          DisableWindow(#ui_main_window, #False)
        EndIf        
    EndSelect
  EndProcedure
     
  Procedure open_exit_window()
    OpenWindow(#ui_exit_window, #PB_Ignore, #PB_Ignore, 190, 100, "Exit ?", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(#exit_btn_no, 10, 50, 80, 45, "No")
    ButtonGadget(#exit_btn_yes, 100, 50, 80, 45, "Yes")
    BindEvent(#PB_Event_Menu, @events_exit_window(), #ui_exit_window)
    BindEvent(#PB_Event_Gadget, @events_exit_window(), #ui_exit_window)
    BindEvent(#PB_Event_CloseWindow, @events_exit_window(), #ui_exit_window)
  EndProcedure
EndModule



; ui_welcome.pbi

Module welcome_window
  
  UseModule exit_window
  UseModule main_window
  
  Procedure welcome_btn_start_handler()
    HideWindow(#ui_welcome_window, #True)
    open_main_window()
  EndProcedure
  
  Procedure events_welcome_window()
    Select Event()
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #welcome_btn_start : CloseWindow(#ui_welcome_window) : open_main_window()
          EndSelect
        Case #PB_Event_CloseWindow
          DisableWindow(#ui_welcome_window, #True)
          open_exit_window()
      EndSelect
  EndProcedure
  
  Procedure open_welcome_window()
    OpenWindow(#ui_welcome_window, #PB_Ignore, #PB_Ignore, 300, 400, "Welcome Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(#welcome_btn_start, 10, 360, 280, 30, "Start")
    BindEvent(#PB_Event_Menu, @events_welcome_window(), #ui_welcome_window)
    BindEvent(#PB_Event_Gadget, @events_welcome_window(), #ui_welcome_window)
    BindEvent(#PB_Event_CloseWindow, @events_welcome_window(), #ui_welcome_window)
  EndProcedure

EndModule



; ui_main.pbi

Module main_window
  
  UseModule exit_window
  
  Procedure events_main_window()
    Select Event()
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #main_btn_1 : Debug "#main_btn_1"
            Case #main_btn_2 : Debug "#main_btn_2"
          EndSelect
        Case #PB_Event_CloseWindow
          DisableWindow(#ui_main_window, #True)
          open_exit_window()
      EndSelect
  EndProcedure
  
  Procedure open_main_window()
    OpenWindow(#ui_main_window, #PB_Ignore, #PB_Ignore, 600, 400, "Main Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(#main_btn_1, 10, 360, 280, 30, "Button 1")
    ButtonGadget(#main_btn_2, 300, 360, 280, 30, "Button 2")
    BindEvent(#PB_Event_Menu, @events_main_window(), #ui_main_window)
    BindEvent(#PB_Event_Gadget, @events_main_window(), #ui_main_window)
    BindEvent(#PB_Event_CloseWindow, @events_main_window(), #ui_main_window)
  EndProcedure
EndModule

UseModule declare_program
UseModule exit_window
UseModule welcome_window
UseModule main_window

If *mydata\welcome = #True
  open_welcome_window()
Else
  open_main_window()
EndIf

Repeat : WaitWindowEvent() : ForEver