The case of the disappearing gadgets- Help me solve it

Just starting out? Need help? Post your questions and find answers here.
PrietoM
New User
New User
Posts: 9
Joined: Sun Oct 04, 2009 10:58 pm
Location: Trying to jump the border into USA

The case of the disappearing gadgets- Help me solve it

Post by PrietoM »

I am working on a program for my work. I have encountered a major roadblock. I have 2 windows with some gadgets. The program first opens the main window #wndMain and then after clicking a button it opens another window called #wndQueryView. When the second window opens a text and a String gadget disappear (#header and #txtpath) from the first window. I searched the forums and the documentation and for the life of me I can't find the reason. I was able to stripped down my code to re-create issue. Mac OS 10.8.5 with PureBasic 5.20 LTS (MacOS X - x64)
Please help!!!!

See code below:

Code: Select all

EnableExplicit
UseSQLiteDatabase()

Define wndEvent.i
Global quitApp.i,workingdb.s

;Settings and colors
#textHeight = 25
#wndColor = $D5E6E5
#errorColor = $C0C1F0
#correctEntry = $D4EDC0

;Main objects enumerations
Enumeration
  #db
  #wndMain
  #mainContainer
  #wndQueryView
  #listResults
EndEnumeration

;Gadgets enumeration
Enumeration
  #header
  #strDBPath
  #txtDBPath
  #btnGetDB
  #txtField
  #listFields
  #txtContains
  #frame
  #strLike
  #btnView
  #courier15
  #courier12
EndEnumeration

;Gadgets for Window query view
Enumeration
  #viewHeader
  #btnCheck
  #btnUncheck
EndEnumeration


Procedure queryView(query.s)
  ;If it can open the database and can query it
  ;Then the Query Result window will show.
  
  Define recsmsg.s
   
       recsmsg = "Tickets found: 0"
    
      ; Open window to show results of query
      If OpenWindow(#wndQueryView,0,0,700,600,"Query Results",#PB_Window_SystemMenu|#PB_Window_WindowCentered,WindowID(#wndMain))
        SetWindowColor(#wndQueryView,#wndColor)
        
        TextGadget(#viewHeader,250,20,200,#textHeight,recsmsg,#PB_Text_Center)
        SetGadgetFont(#viewHeader,FontID(#courier15))
        
        ;Create listicongadget
        ListIconGadget(#listResults,10,40,680,500,"ID",50,#PB_ListIcon_CheckBoxes|#PB_ListIcon_ThreeState|#PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection)
        AddGadgetColumn(#listResults,1,"Summary",250)
        AddGadgetColumn(#listResults,2,"Description",380)
        
        ;Add buttons check and uncheck
        ButtonGadget(#btnUncheck,20,545,105,#textHeight,"Uncheck All")
        SetGadgetFont(#btnUncheck,FontID(#courier12))
        
        
        ButtonGadget(#btnCheck,135,545,100,#textHeight,"Check All")
        SetGadgetFont(#btnCheck,FontID(#courier12))
  
  EndIf
  
EndProcedure

Procedure wndMain()
  ;Open main window and create all the needed gadgets
  
  If OpenWindow(#wndMain,0,0,800,350,"Ticket Management",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) 
    SetWindowColor(#wndMain,#wndColor)
    
    TextGadget(#header,250,20,300,#textHeight,"Spiceworks Bulk Ticket Delete",#PB_Text_Center)
    
    FrameGadget(#frame,25,50,750,150,"",#PB_Frame_Flat)
    
    TextGadget(#txtdbPath,60,60,80,#textHeight,"DB Path:",#PB_Text_Right)
    StringGadget(#strDBPath,145,60,350,#textHeight,"")
    
    ButtonGadget(#btnGetDB,500,60,100,#textHeight,"Get DB")
    ;BindGadgetEvent(#btnGetDB,@getDBPath())                 ; Call Back
    
    TextGadget(#txtField,50,120,100,#textHeight,"Where Field:")
    ComboBoxGadget(#listFields,160,120,125,25)
    AddGadgetItem(#listFields,0,"Summary")
    AddGadgetItem(#listFields,1,"Description")
    SetGadgetState(#listFields,0)         ; Set the first text in combo box
    
    TextGadget(#txtContains,280,120,80,#textHeight,"Contains",#PB_Text_Center)
    
    StringGadget(#strLike,365,120,300,#textHeight,"")
    
    ButtonGadget(#btnView,670,120,75,#textHeight,"View")
    BindGadgetEvent(#btnView,@queryView())                 ; Callback
    
    ;Load fonts And apply To gadgets
    If LoadFont(#courier15,"Courier",15,#PB_Font_Bold)
      SetGadgetFont(#header,FontID(#courier15))
    EndIf
    
    If LoadFont(#courier12,"Courier",12)
      SetGadgetFont(#txtDBPath,FontID(#courier12))
      SetGadgetFont(#strDBPath,FontID(#courier12))
      SetGadgetFont(#btnGetDB,FontID(#courier12))
      SetGadgetFont(#txtField,FontID(#courier12))
      SetGadgetFont(#txtContains,FontID(#courier12))
      SetGadgetFont(#strLike,FontID(#courier12))
    EndIf
    
   
  EndIf
  
EndProcedure

;-------------- Main Program -------------------
wndMain()

Repeat
  wndEvent = WindowEvent()
  
  Select wndEvent
      
    Case #PB_Event_CloseWindow
      
      Select EventWindow()
        Case #wndMain
          quitApp = 1
        Case #wndQueryView
          CloseWindow(#wndQueryView)
          
      EndSelect
      
  EndSelect
    
Until quitApp = 1
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: The case of the disappearing gadgets- Help me solve it

Post by Demivec »

PrietoM wrote: When the second window opens a text and a String gadget disappear (#header and #txtpath) from the first window.
You are using the same GadgetID's for different gadgets. When you reuse the value it replace the previous gadget with the newest one. This is what you are seeing when a former gadget disappears.

Gadget numbers have to be unique even when they are in different windows.

One way to correct it is to use #PB_Any to generate new an unique identifiers for gadgets that are created. Another and simpler way to correct it for your particular situation is by using a 'Named Enumeration' like so:

Code: Select all

Enumeration
  #db
  #wndMain
  #mainContainer
  #wndQueryView
  #listResults
EndEnumeration

;Gadgets enumeration
Enumeration Gadgets ;this will start numbering with zero
  #header
  #strDBPath
  #txtDBPath
  #btnGetDB
  #txtField
  #listFields
  #txtContains
  #frame
  #strLike
  #btnView
  #courier15
  #courier12
EndEnumeration

;Gadgets for Window query view
Enumeration Gadgets ;this will continue the former 'Gadgets' enumeration where the former left off (at 12)
  #viewHeader
  #btnCheck
  #btnUncheck
EndEnumeration
You can also group the gadgets for each window together in the same enumeration.
PrietoM
New User
New User
Posts: 9
Joined: Sun Oct 04, 2009 10:58 pm
Location: Trying to jump the border into USA

Re: The case of the disappearing gadgets- Help me solve it

Post by PrietoM »

Demivec, thanks for your reply??
I put all the gadgets under one Enumeration and the code works!!
What is the difference between putting the gadgets in one enumeration vs multiple enumerations?? I though PB will assign a number based on the last enumeration number??
Please explain the difference between these two snippets.

Code: Select all

Enumeration
  #gadget0
  #gadget1
EndEnumeration

Enumeration
 #gadget3
 #gadget4
EndEnumeration
vs this code:

Code: Select all

Enumeration
  #gadget0
  #gadget1
 #gadget3
 #gadget4
EndEnumeration
I am just begining with PB and I think this is important for me to learn!!!
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: The case of the disappearing gadgets- Help me solve it

Post by Demivec »

PrietoM wrote:What is the difference between putting the gadgets in one enumeration vs multiple enumerations?? I though PB will assign a number based on the last enumeration number??
First, here is a link to the help manual section on Enumerations.

Each time you start an enumeration you can specify what value it starts with and the step between values. If these aren't specified it will default to starting with zero and increase the values by 1 for successive constants.

Using a named enumeration will cause the enumeration with the given name (i.e. 'Gadgets', or 'myValues', etc.) to start with the next value after the last enumerated value in a previous enumeration that used the same name.

The difference between the following codes I'll describe in their comments.

Example one:

Code: Select all

Enumeration      ;Initial value zero, step 1
  #gadget0     ; = 0
  #gadget1     ; = 1
EndEnumeration

Enumeration               ;Initial value zero, step 1
  #gadget3     ; = 0
  #gadget4     ; = 1
EndEnumeration
Example two:

Code: Select all

Enumeration       ;Initial value zero, step 1
  #gadget0     ; = 0
  #gadget1     ; = 1
  #gadget3     ; = 2
  #gadget4     ; = 3
EndEnumeration
Example three:

Code: Select all

Enumeration 5      ;Initial value 5, step 1
  #gadget0     ; = 5
  #gadget1     ; = 6
EndEnumeration

Enumeration 9 step 3      ;Initial value 9, step 3
  #gadget3     ; = 9
  #gadget4     ; = 12
EndEnumeration
Example four:

Code: Select all

Enumeration myValues   ;named enumeration, initial value 0, step 1
  #gadget0      ; = 0
  #gadget1      ; = 1
EndEnumeration

Enumeration                         ;initial value 0, step 1
  #something1  ; = 0
  #something2  ; = 1
EndEnumeration

Enumeration myValues           ;named enumeration continues with initial value of 2, step 1
  #gadget3      ; = 2
  #gadget4      ; = 3
EndEnumeration
Example five:

Code: Select all

Enumeration                ;initial value zero, step 1
  #gadget0      ; = 0
  #gadget1      ; = 1
EndEnumeration

Enumeration                         ;initial value zero, step 1
 #gadget3 = 3  ; = 3, set value to 3 and continue from there at the previous step value
 #gadget4       ; = 4
EndEnumeration
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: The case of the disappearing gadgets- Help me solve it

Post by luis »

PrietoM wrote:I though PB will assign a number based on the last enumeration number??
You thought. Why ? The help didn't tell you so. Why did you assume it ?
help wrote: The first constant found in the enumeration will get the number 0 and the next one will be 1 etc.
Pretty clear.
help wrote: The reserved constant #PB_Compiler_EnumerationValue store the next value which will be used in the enumeration.
It can be useful to get the last enumeration value or to chain two enumerations.
To chain two enumerations. It's saying that to do what you thought was happening you need to request it explicitly.
help wrote: A name can be set to identify an enumeration and allow to continue it later
Again another way to explicitly ask for what you thought was happening.

If you had still doubts after reading the manual, you could have verified what PB *actually* do by using a simple debug statement and print the values of the constants defined in the enumerations.
PrietoM wrote:I am just begining with PB and I think this is important for me to learn!!!
Reading all the manual from start to finish once is a good step in that direction.

Moreover the "Purebasic objects overview" of the manual says:
help wrote: An object that is associated with an index is automatically freed when reusing that index
This means if you use a gadget, and then reuse the same numeric id for another gadget (of the same family), the newer gadget replace the first one which is also freed.
Knowing that, you could have some suspects about why some gadgets were disappearing. From there to the idea to check if the constants were really what you expected them to be, the step is small.

See, it was all there :wink:
"Have you tried turning it off and on again ?"
A little PureBasic review
TheMexican
New User
New User
Posts: 6
Joined: Mon Nov 25, 2013 7:19 pm

Re: The case of the disappearing gadgets- Help me solve it

Post by TheMexican »

I did read the manual, but somehow I understood :oops: that the enumeration will continue based on the last enumerated item.

Sometimes the way the help file is written, it takes a little bit of asking the experts (this forum) for things to make sense.

For example, it took me some time to figure out how to uncheck a checkbox on a ListIconGadget(). It was pretty easy to figure it how to check them, but to unchecked them took me a while. Here is an example from the Documentation:
SetGadgetItemState(#Gadget, Item, State)
ListIconGadget(): Combination of the following values:

#PB_ListIcon_Selected : The item should be selected.
#PB_ListIcon_Checked : The item should have its checkbox checked (#PB_ListIcon_CheckBoxes flag).
#PB_ListIcon_Inbetween: The item should have its checkbox in the in between state (#PB_ListIcon_ThreeState flag).
Pretty easy to understand how to have the checkbox "check". Iterate through items and then use SetGadgetItemState()

But how to uncheck all items on a ListIconCheckbox?
I was expecting a PB constant like #PB_ListIcon_Un_Checked. Finally I came across this that says
The #PB_ListIcon_ThreeState flag can be used in combination with the #PB_ListIcon_CheckBoxes flag to get checkboxes that can have an "on", "off" and "in between" state. The user can only select the "on" or "off" states. The "in between" state can be set programmatically using the SetGadgetItemState() function.
Which in order to change to an uncheck state the constant to use is 0. I also learned that you don't have to use the #PB_ListIcon_ThreeState if I don't need a tri-state.
All I am saying is that for us PB beginners, what may seem obvious sometimes is not, and we need your help. Which by the way you guys answer pretty fast!!

Thanks for your help!!
Post Reply