Gadget arrays

Just starting out? Need help? Post your questions and find answers here.
Doctor Watson
User
User
Posts: 10
Joined: Sun Feb 14, 2010 2:37 pm

Gadget arrays

Post by Doctor Watson »

Hello.

Beginners question (there will be more of those if you don't mind ... :mrgreen: )
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
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4794
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Gadget arrays

Post by Fangbeast »

Doctor Watson wrote:Hello.

Beginners question (there will be more of those if you don't mind ... :mrgreen: )
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
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Gadget arrays

Post 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.
oh... and have a nice day.
Fred
Administrator
Administrator
Posts: 18384
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Gadget arrays

Post 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
Doctor Watson
User
User
Posts: 10
Joined: Sun Feb 14, 2010 2:37 pm

Re: Gadget arrays

Post 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 :mrgreen:
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4794
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Gadget arrays

Post 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:):)
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Gadget arrays

Post 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 :D

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!
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Gadget arrays

Post 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).
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Gadget arrays

Post 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.
oh... and have a nice day.
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Gadget arrays

Post by UserOfPure »

I know. For beginners, it's good because they don't have to think. :)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Gadget arrays

Post 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.
oh... and have a nice day.
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Gadget arrays

Post by UserOfPure »

Nothing is impossible. Just a hell of a lot more code to do it. ;)
BWCroft
New User
New User
Posts: 4
Joined: Thu Feb 18, 2010 3:37 pm

Re: Gadget arrays

Post 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
Doctor Watson
User
User
Posts: 10
Joined: Sun Feb 14, 2010 2:37 pm

Re: Gadget arrays

Post 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 ... :D

Michel
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Gadget arrays

Post 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. :)
oh... and have a nice day.
Post Reply