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

Just starting out? Need help? Post your questions and find answers here.
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

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

Post 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
Sergey
User
User
Posts: 60
Joined: Wed Jan 12, 2022 2:41 pm

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

Post by Sergey »

You may use SetActiveWindow(Main_Window) after CloseWindow(ChildWindow) :wink:
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply