It’s quite possible to structure your PureBasic code so all the routines that pertain to a particular window are kept together:
EnableExplicit
Enumeration windows
#frmMain
#frmChild
EndEnumeration
Enumeration gadgets
#frmMain_btnChild
#frmChild_btnOK
EndEnumeration
;-child window layout
Procedure frmChild_Open()
If OpenWindow(#frmChild, 250, 50, 200, 250, "Form 2", #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_SizeGadget)
ButtonGadget(#frmChild_btnOK, 10, 10, 180, 30, "Click Me!")
EndIf
EndProcedure
;-child window gadget event handlers
Procedure frmChild_onGadget(nGadget, nEventType, nX, nY)
Select nGadget
Case #frmChild_btnOK
MessageRequester("Message", "Hello, World!", 0)
EndSelect
EndProcedure
;-child window resize event handler
Procedure frmChild_onResize(nW, nH)
ResizeGadget(#frmChild_btnOK, #PB_Ignore, #PB_Ignore, nW-20, #PB_Ignore)
EndProcedure
;-main window layout
Procedure frmMain_Open()
If OpenWindow(#frmMain, 50, 50, 400, 150, "Form 1", #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_SizeGadget)
ButtonGadget(#frmMain_btnChild, 10, 10, 380, 30, "Show Form 2")
EndIf
EndProcedure
;-main window gadget event handlers
Procedure frmMain_onGadget(nGadget, nEventType, nX, nY)
Select nGadget
Case #frmMain_btnChild
frmChild_Open()
EndSelect
EndProcedure
;-main window resize event handler
Procedure frmMain_onResize(nW, nH)
ResizeGadget(#frmMain_btnChild, #PB_Ignore, #PB_Ignore, nW-20, #PB_Ignore)
EndProcedure
;-routing of gadget events
Procedure Events_Gadget()
Define.i nGadget = EventGadget(),
nEventType = EventType(),
nX = 0,
nY = 0
Select GadgetType(nGadget)
Case #PB_GadgetType_Canvas
nX = GetGadgetAttribute(nGadget, #PB_Canvas_MouseX)
nY = GetGadgetAttribute(nGadget, #PB_Canvas_MouseY)
Case #PB_GadgetType_ListIcon
nY = GetGadgetState(nGadget)
nX = GetGadgetAttribute(nGadget, #PB_ListIcon_ClickedColumn)
; etc...
EndSelect
Select EventWindow()
Case #frmMain
frmMain_onGadget(nGadget, nEventType, nX, nY)
Case #frmChild
frmChild_onGadget(nGadget, nEventType, nX, nY)
EndSelect
EndProcedure
;-handle window close events
Procedure Events_CloseWindow()
Define.i nWin = EventWindow()
Select nWin
Case #frmMain
End
Default
CloseWindow(nWin)
EndSelect
EndProcedure
;-routing of window resize events
Procedure Events_ResizeWindow()
Define.i nWin = EventWindow(),
nW = WindowWidth(nWin),
nH = WindowHeight(nWin)
Select nWin
Case #frmMain
frmMain_onResize(nW, nH)
Case #frmChild
frmChild_onResize(nW, nH)
EndSelect
EndProcedure
; -bind events to handlers
BindEvent(#PB_Event_Gadget, @Events_Gadget())
BindEvent(#PB_Event_CloseWindow, @Events_CloseWindow())
BindEvent(#PB_Event_SizeWindow, @Events_ResizeWindow())
;-execution begins here
frmMain_Open()
Repeat
Define.i nWait = WaitWindowEvent()
ForEver