Page 1 of 1

how to create a child window correctly

Posted: Mon Sep 07, 2020 4:37 pm
by k3pto
I would like to create several child windows and select which one is activated via buttons in the parent window. The following code does not work as I expected. I was hoping to create all the child windows and simply hide them until needed.

Code: Select all

Enumeration
  #MainWindow
  #ChildWindow
  #MainButton1
  #MainButton2
  #ChildButton1
  #ChildButton2
EndEnumeration

; open main window and create one button
OpenWindow(#MainWindow, 0, 0, 1000, 580, "Main window", #PB_Window_SystemMenu | #PB_Window_TitleBar)
SetWindowColor(0, RGB(191,255,255))
  
ButtonGadget( #MainButton1, 200, 100, 50, 30, "MB1 " )
  
; open child window and create one button  
OpenWindow(#ChildWindow, 100, 100, 500, 400, "Child Window", #PB_Window_TitleBar, WindowID(#MainWindow))
ButtonGadget( #ChildButton1, 300, 200, 50, 30, "CB 1" )

; hide child window and create another button in main window
HideWindow(#ChildWindow, #True )
SetActiveWindow(#MainWindow)
ButtonGadget( #MainButton2, 300, 300, 50, 30, "MB 2" ) ;<<<< button appears in the child window

Repeat
  Event = WaitWindowEvent()

  Select Event
    Case #PB_Event_Gadget
      i = GetActiveGadget ()
      Select i
        Case #MainButton1
          SetActiveWindow( #ChildWindow )
          HideWindow(#ChildWindow, #False )        
        Case #ChildButton1
          HideWindow(#ChildWindow, #True )
      EndSelect
  EndSelect
  
Until Event = #PB_Event_CloseWindow ; Quit on any window close
From these results, it appears that I have to create and populate each child whenever it is to be activated. Is that the case :?:

Re: how to create a child window correctly

Posted: Mon Sep 07, 2020 4:58 pm
by RASHAD
Hi
Try MDIGadget() it may help

Re: how to create a child window correctly

Posted: Mon Sep 07, 2020 5:30 pm
by infratec
You have to re-use the GadgetList.

Also your GetActiveGadget() is wrong.

Code: Select all

Enumeration
  #MainWindow
  #ChildWindow
  #MainButton1
  #MainButton2
  #ChildButton1
  #ChildButton2
EndEnumeration

; open main window and create one button
OpenWindow(#MainWindow, 0, 0, 1000, 580, "Main window", #PB_Window_SystemMenu | #PB_Window_TitleBar)
SetWindowColor(0, RGB(191,255,255))
ButtonGadget( #MainButton1, 200, 100, 50, 30, "MB1 " )

; open child window and create one button 
OpenWindow(#ChildWindow, 100, 100, 500, 400, "Child Window", #PB_Window_TitleBar, WindowID(#MainWindow))
ButtonGadget( #ChildButton1, 300, 200, 50, 30, "CB 1" )

; hide child window and create another button in main window
HideWindow(#ChildWindow, #True )
SetActiveWindow(#MainWindow)

UseGadgetList(WindowID(#MainWindow))
ButtonGadget(#MainButton2, 300, 300, 50, 30, "MB 2" ) ;<<<< button appears in the child window

Repeat
  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #MainButton1
          SetActiveWindow( #ChildWindow )
          HideWindow(#ChildWindow, #False )       
        Case #ChildButton1
          HideWindow(#ChildWindow, #True )
      EndSelect
  EndSelect
  
Until Event = #PB_Event_CloseWindow ; Quit on any window close
Btw. Up to now no answer if your RS232 stuff works now or not.

Re: how to create a child window correctly

Posted: Thu Sep 24, 2020 12:54 am
by k3pto
Thanks for all your help. I have finally decided to simply stay with regular windows and make sure I have all the gadgets for the main window defined before I activate another window.

Re: how to create a child window correctly

Posted: Thu Sep 24, 2020 9:18 am
by NicTheQuick
If you are working with multiple windows you should also use EventWindow() to determine in which window the event occured. This is helpful if you want to see which window has moved, got resized or on which one the close button was clicked.

Re: how to create a child window correctly

Posted: Thu Sep 24, 2020 3:20 pm
by k3pto
Hi NicTheQuick, Thanks for the "heads up."