how to create a child window correctly

Just starting out? Need help? Post your questions and find answers here.
k3pto
User
User
Posts: 50
Joined: Sat Jan 17, 2015 5:24 pm

how to create a child window correctly

Post 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 :?:
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4659
Joined: Sun Apr 12, 2009 6:27 am

Re: how to create a child window correctly

Post by RASHAD »

Hi
Try MDIGadget() it may help
Egypt my love
infratec
Always Here
Always Here
Posts: 6867
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: how to create a child window correctly

Post 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.
k3pto
User
User
Posts: 50
Joined: Sat Jan 17, 2015 5:24 pm

Re: how to create a child window correctly

Post 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.
User avatar
NicTheQuick
Addict
Addict
Posts: 1226
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: how to create a child window correctly

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
k3pto
User
User
Posts: 50
Joined: Sat Jan 17, 2015 5:24 pm

Re: how to create a child window correctly

Post by k3pto »

Hi NicTheQuick, Thanks for the "heads up."
Post Reply