Problems with getting windows to open.

Just starting out? Need help? Post your questions and find answers here.
MPrimal
User
User
Posts: 15
Joined: Fri Nov 11, 2005 4:54 pm
Location: England

Problems with getting windows to open.

Post by MPrimal »

I have created some windows in PureVisionXP and got them exported but for some reason when I call them, they always fail to appear (with exception of my main window.

The window I am trying to open is created like this:

Code: Select all

Procedure.l Window_NewEntry1()
 Window_NewEntry1=OpenWindow(#PB_Any,101,63,743,656,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible,"Add New Entry - Character Selection")
  If Window_NewEntry1
    If CreateGadgetList(WindowID(Window_NewEntry1))
      Gadget_NewEntry1_Text_SelChar=TextGadget(#PB_Any,280,15,180,30,"Select Character",#PB_Text_Center|#PB_Text_Border)
        PVDynamic_AddColorGadget(Gadget_NewEntry1_Text_SelChar,8388863,0)
        SetGadgetFont(Gadget_NewEntry1_Text_SelChar,UseFont(LoadFont(#PB_Any,"Arial",14,260)))
      Gadget_NewEntry1_Char_Select=ListIconGadget(#PB_Any,210,50,310,595,"Character",200,#PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection)
        AddGadgetColumn(Gadget_NewEntry1_Char_Select,1,"Series",100)
      Gadget_NewEntry1_ANE1_Confirm=ButtonGadget(#PB_Any,625,615,85,25,"Confirm")
      Gadget_NewEntry1_ANE1_Cancel=ButtonGadget(#PB_Any,25,615,85,25,"Cancel Entry")
      Gadget_NewEntry1_Image_TFDB1=ImageGadget(#PB_Any,575,125,128,96,UseImage(Image_NewEntry1_Image_TFDB1))
      Gadget_NewEntry1_Image_TFDB2=ImageGadget(#PB_Any,25,125,128,96,UseImage(Image_NewEntry1_Image_TFDB2))
      HideWindow(Window_NewEntry1,0)
      ProcedureReturn WindowID()
    EndIf
  EndIf
EndProcedure
This is part of the file TFDB_Windows.pb which Purevision created for me.

The window is controled by this routine:

Code: Select all

  Window_NewEntry1()
   
  Repeat
    
    Select GadgetID
            
      Case Gadget_NewEntry1_Char_Select
        EntrySelect=GetGadgetState(Gadget_NewEntry1_Char_Select)      
         
    Case Gadget_NewEntry1_ANE1_Confirm
      If EntrySelect=-1
        Result = MessageRequester("No Entry Selected","You Must Select A Character From The List!",#PB_MessageRequester_Ok)
      Else
        Exit = 1
      EndIf
      
    Case Gadget_NewEntry1_ANE1_Cancel
     Exit = 2
      
 EndSelect
 
 Select EventID
      
    Case #PB_EventCloseWindow
     Exit = 2
   
 EndSelect
  
  Until Exit <> 0
   
  CloseWindow (Window_NewEntry1)
When I run the program and press the button that opens this new window I get the following error:

[ERROR] #Gadget object not initialized

It points to the line that reads:

EntrySelect=GetGadgetState(Gadget_NewEntry1_Char_Select)

as the problemwhen this error occurs


Can someone explain to me what is going on here and how I might fix it.

Thanks

Max
Real Power Comes From Sharing It With Those Who Think They Have It All.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

The most obvious problem is that you are trying to access variables that have been created within the procedure Window_NewEntry1(), which are local to the procedure and thus are only valid within the procedure itself.

For example, because 'Gadget_NewEntry1_Char_Select' is defined within the procedure, it's value is lost as soon as the procedure is finished. Thus, when the statement

Code: Select all

EntrySelect=GetGadgetState(Gadget_NewEntry1_Char_Select) 
is executed, the value of 'Gadget_NewEntry1_Char_Select' has been lost and does not reference a gadget.

One solution is to use global variables.

Use the command:

Code: Select all

Global Gadget_NewEntry1_Char_Select.l
before the procedure begins.

You will need to do this for all variables which you intend to use both inside and outside of the procedure.

If truth be told, relying upon global variables is not 'good' practice as they can be altered by any code anywhere in the application and can lead to some very hard to trace bugs. A better solution would be to remove the procedure, or at least create the gadgets outside of the procedure, therefore removing the need for global variables. Alternatively, stick the event loop within the procedure and thus 'wrap' the entire window etc. Alternatively, pass the address of a structure to the procedure which is to contain all the #gadget identifiers. :D

Regards.
I may look like a mule, but I'm not a complete ass.
Post Reply