Page 1 of 1
Gadget arrays
Posted: Tue Feb 16, 2010 9:09 am
by Doctor Watson
Hello.
Beginners question (there will be more of those if you don't mind ...

)
Is it possible to have an array of gadgets, so that they can be created at runtime? Let's say I need to create a variable number of StringGadgets at runtime, how should I go about that ?
Greetings
Michel
Re: Gadget arrays
Posted: Tue Feb 16, 2010 9:58 am
by Fangbeast
Doctor Watson wrote:Hello.
Beginners question (there will be more of those if you don't mind ...

)
Is it possible to have an array of gadgets, so that they can be created at runtime? Let's say I need to create a variable number of StringGadgets at runtime, how should I go about that ?
Greetings
Michel
Code: Select all
Global Dim GadgetArray.i(0) ; Create initial array, global so you can acces the contents anywhere
Procedure MakeGadgets(NumberofGadgets.i)
ReDim GadgetArray.i(NumberofGadgets.i) - 1
For GadgetStart = 0 To NumberofGadgets.i - 1
GadgetArray(GadgetStart) = StringGadget(#PB_Any, 0, 0, 120, 25, "")
Next
EndProcedure
MakeGadgets(12)
You now have a Global Array of Stringgadgets that contain unique handles that you can access whenever you need. Not sure how you would handle dimesions and placement if they are created on the fly though.
However, using the ResizeGadget(Handle, x, y, Width, Height) would work on gadgets in the array if you knew where the gadgets had to go.go
Re: Gadget arrays
Posted: Tue Feb 16, 2010 10:07 am
by Kaeru Gaman
you can also use a LinkedList to store the gadgets' informations.
the key is the use of #PB_Any, like Fangles showed.
Re: Gadget arrays
Posted: Tue Feb 16, 2010 11:36 am
by Fred
You don't need to use an array, as you can use #Gadget index for this:
Code: Select all
For k = 0 To 4
StringGadget(k, 0, k*30, 200, 25, "") ; Gadget numbered from 0 to 4 are created.
Next
Re: Gadget arrays
Posted: Tue Feb 16, 2010 12:37 pm
by Doctor Watson
Oh dear ! This looks even easier than I thought.
The reason for my question is a project I'm working on in RealBasic and I'm handling a similar kind of array there. In RB - because of it's different approach - it's quite straightforward but your solution Fred looks even easier than in RB. Now, if this turns out like I hope it will then I'm seriously considering rewriting the thing in PureBasic.
Hmm... One shouldn't be over-enthusiastic. Remember the African elephant in my first topic

Re: Gadget arrays
Posted: Tue Feb 16, 2010 12:47 pm
by Fangbeast
Fred wrote:You don't need to use an array, as you can use #Gadget index for this:
Code: Select all
For k = 0 To 4
StringGadget(k, 0, k*30, 200, 25, "") ; Gadget numbered from 0 to 4 are created.
Next
Don't wish to dispute the master but if you have other gadgets already and then generate this lot, there is the danger of gadget id collision by hardcoding the numbers.

:):) But then, I have no idea:):)
Re: Gadget arrays
Posted: Tue Feb 16, 2010 4:33 pm
by Rook Zimbabwe
In my POS software I had a number of MENU_PAGE buttons on my main window and I could turn them on and off depending on how they are set in the BD...
Code: Select all
; *** FIRST I open the DB and read the MENU_PAGE items and throw them into a DIM$
Procedure DBQueryMENUITEM()
mi = 0
DatabaseQuery(#Database, "SELECT * FROM MENUITEM;") ;
While NextDatabaseRow(#Database)
h$ = GetDatabaseString(#Database, 1) ; NOTES COLUMN
;Debug "LINE: "+h$
ITEMS$(0 + mi) = h$
mi = mi + 1
h$ = ""
Wend
FinishDatabaseQuery(#Database)
EndProcedure
; **** LOTS more code is here
; *** I open the window
; *** NEXT I set the buttons and their text IGNORING the inactive!
For S = #Button_SEL0 To #Button_SEL12
SetGadgetText(S, ITEMS$(Z))
Molly$ = GetGadgetText(#Button_SEL0 + Z)
Result$ = Left(Molly$, 8)
If Result$ = "INACTIVE"
HideGadget(S, 1) ; **** 1 is hidden and 0 is not
;DisableGadget(#Button_SEL0 + Z, 1)
EndIf
Z = Z + 1
Next
; *** then I run the program
QED
Something similar could be done with your program I am sure! I would advise you to keep all the string boxes named something you can remember easy... it does make it much simpler in the long run to see exactly what you are doing!
Re: Gadget arrays
Posted: Tue Feb 16, 2010 8:22 pm
by UserOfPure
Fangbeast wrote: there is the danger of gadget id collision by hardcoding the numbers
Not if you keep track of them correctly. Using tracked numbers is just as clean and safe as #PB_Any. A good coder will always know what his app is doing, rather than just using #PB_Any so he doesn't have to think. (Note: Not saying #PB_Any is evil, just demonstrating a point).
Re: Gadget arrays
Posted: Wed Feb 17, 2010 12:13 am
by Kaeru Gaman
this is definitely correct.
... but I seen so much fishy crap constructed by beginners when they didn't use #PB_Any, that I generally recommend using #PB_Any for dynamic gadgets.
Re: Gadget arrays
Posted: Wed Feb 17, 2010 9:56 am
by UserOfPure
I know. For beginners, it's good because they don't have to think.

Re: Gadget arrays
Posted: Wed Feb 17, 2010 11:24 am
by Kaeru Gaman
also, it depends on what you want do create.
if you have dynamic forms that can be opened five, six at the same time, it's impossible to solve without #PB_Any.
Re: Gadget arrays
Posted: Wed Feb 17, 2010 12:15 pm
by UserOfPure
Nothing is impossible. Just a hell of a lot more code to do it.

Re: Gadget arrays
Posted: Thu Feb 18, 2010 3:42 pm
by BWCroft
I use enumerations for my 'fixed' gadgets, then after last the enumeration of these I add one called #LastEnumeration.
You can then use Fred's loop but use 'For k = #LastEnumeration to #LastEnumeration+NumberOfNewGadgetsRequired'
Hope this makes sense
Barney
Re: Gadget arrays
Posted: Fri Feb 19, 2010 7:39 am
by Doctor Watson
Thanks everybody for all the advice.
It seems that I will have to do some reading (or trial and error) before I can get the hang of this ...
Michel
Re: Gadget arrays
Posted: Fri Feb 19, 2010 10:26 am
by Kaeru Gaman
just keep in mind, that every Gadget needs a unique ID.
when you create Gadgets by Number, constants or not, you have to take care on your own not to reuse a number.
if you do, nothing horrible will happen, just the first gadget will vanish.
creating Gadgets by numbers will open an internal handle table, where the handles of the Gadgets are stored.
this Table is rather an Array than a List, so when you create a single Gadget with number 5000,
you will reserve a table for 5001 handles at the same time.
thus, working with "speaking code" number ranges may not be the best solution.
(this applies for every PB object that is identified by number, Windows, Sprites, Sounds, Fonts, etc)
working with #PB_Any frees you of these problems, but then you need to store the handles yourself.
the returnvalue of the creating function is the internal handle of the gadget.
also this is the ID you check with EventGadget() like you otherwise check the number.
one more detail:
no matter you use numbers or internal handles, using GadgetID() on it returns the OS-handle of the object.
I think this should clarify a lot.
