Page 1 of 1

Enabling main GUI window and closing child window, sequence is important

Posted: Sun Jul 28, 2024 9:59 am
by PBJim
I'm going through PureBasic's GUI handling with a new colleague and we noticed by chance that when a child window is closed, the sequence of first re-enabling the main window and then closing the child window, is important, otherwise the main window loses focus. In consequence, if other desktop applications are open, the main window becomes concealed.

This can be seen if lines denoted First and Second are reversed. While there is nothing wrong with it, understanding the reason would be useful. Thanks.

Code: Select all

EnableExplicit

Global Main_Window, ChildWindow, ListGadget
Define Event

Enumeration SpecialKeys
  #EntKey
  #EscKey
EndEnumeration

Procedure OpenSecondWindow()
  
  ChildWindow = OpenWindow(#PB_Any, 25, 475, 450, 300, "Second window, press ESCAPE to close", #PB_Window_ScreenCentered)
  ListGadget = ListIconGadget(#PB_Any, 5, 5, 475, 300, "Name", 100, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
  AddGadgetColumn(ListGadget, 1, "Address", 250)
  AddGadgetItem(ListGadget, -1, "Record 1"+Chr(10)+"12 Parliament Way, Battle Street, By the Bay")
  AddGadgetItem(ListGadget, -1, "Record 2"+Chr(10)+"130 PureBasic Road, BigTown, CodeCity")

  AddKeyboardShortcut(ChildWindow, #PB_Shortcut_Escape, #EscKey) ; ESC shortcut (close window)
  
  DisableWindow(Main_Window, #True) ; Disable the main window
  
EndProcedure

Main_Window = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 500, 400, "Main window, Press ENTER to open second window", #PB_Window_MinimizeGadget | #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddKeyboardShortcut(Main_Window, #PB_Shortcut_Return, #EntKey) ; ENTER shortcut (open second window)

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_CloseWindow
      Break
      
    Case #PB_Event_Menu
      
      Select EventMenu()
        Case #EscKey
          If IsWindow(ChildWindow)
            
            DisableWindow(Main_Window, #False)       ; <-------------------- First
            CloseWindow(ChildWindow)                 ; <-------------------- Second
            
          EndIf
        Case #EntKey
          If Not IsWindow(ChildWindow) ; Don't open second window
            OpenSecondWindow()
          EndIf
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget()
          ; Events
      EndSelect
  EndSelect
ForEver

Re: Enabling main GUI window and closing child window, sequence is important

Posted: Sun Jul 28, 2024 12:07 pm
by Sergey
You may use SetActiveWindow(Main_Window) after CloseWindow(ChildWindow) :wink:

Re: Enabling main GUI window and closing child window, sequence is important

Posted: Sun Jul 28, 2024 12:33 pm
by mk-soft
Sergey wrote: Sun Jul 28, 2024 12:07 pm You may use SetActiveWindow(Main_Window) after CloseWindow(ChildWindow) :wink:
better SetActiveWindow(Main_Window) before CloseWindow(ChildWindow) 8)