Newbie stumper

Everything else that doesn't fall into one of the other PB categories.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Newbie stumper

Post by Trond »

It's pretty easy to shove the gadget numbers into a list or map, and then they can be looped over effortlessly:

Code: Select all

OpenWindow(0,100,100,640,480,"test")

NewMap Buttons()

Buttons("username") = ButtonGadget(#PB_Any,10,10,200,25,"Username")
Buttons("password") = ButtonGadget(#PB_Any,10,40,200,25,"Password")
Buttons("website")  = ButtonGadget(#PB_Any,10,70,200,25,"Website")

ForEach Buttons()
  Debug GetGadgetText(Buttons())
Next
Debug "--"

Repeat

  event=WaitWindowEvent()

  If event=#PB_Event_Gadget
    Select EventGadget()
      Case Buttons("username") : Debug "clicked username"
      Case Buttons("password") : Debug "clicked password"
      Case Buttons("website")  : Debug "clicked website"
    EndSelect
  EndIf

Until event=#PB_Event_CloseWindow
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Newbie stumper

Post by MachineCode »

Actually, you CAN use my tip if you simply throw "IsGadget()" into the loop, like below... but it's not recommended.

Code: Select all

OpenWindow(0,100,100,640,480,"test")

Global username=ButtonGadget(#PB_Any,10,10,200,25,"Username")
Global password=ButtonGadget(#PB_Any,10,40,200,25,"Password")
Global website=ButtonGadget(#PB_Any,10,70,200,25,"Website")

Repeat

  event=WaitWindowEvent()

  If event=#PB_Event_Gadget
    For g=username To website
      If IsGadget(g) : Debug GetGadgetText(g) : EndIf
    Next
  EndIf

Until event=#PB_Event_CloseWindow
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Re: Newbie stumper

Post by Arctic Fox »

@MachineCode
What if the assigned id value for username is larger than website?
User avatar
Kuron
Addict
Addict
Posts: 1626
Joined: Sat Oct 17, 2009 10:51 pm
Location: Pacific Northwest

Re: Newbie stumper

Post by Kuron »

MachineCode wrote:
IdeasVacuum wrote:if you have 25 "#PB_Any String Gadgets" I don't see how that is easy to understand in your code in terms of each gadgets purpose?
It's easy! Do what I do. No enumerations required! :)

Code: Select all

OpenWindow(0,100,100,640,480,"test")

Global username=ButtonGadget(#PB_Any,10,10,200,25,"Username")
Global password=ButtonGadget(#PB_Any,10,40,200,25,"Password")
Global website=ButtonGadget(#PB_Any,10,70,200,25,"Website")

Repeat

  event=WaitWindowEvent()

  If event=#PB_Event_Gadget
    Select EventGadget()
      Case username : Debug "username"
      Case password : Debug "password"
      Case website  : Debug "website"
    EndSelect
  EndIf

Until event=#PB_Event_CloseWindow
This would be a nightmare for any decent sized application. I can't imagine having to remember (and give) unique names for hundreds of gadgets. :shock:
Best wishes to the PB community. Thank you for the memories. ♥️
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Newbie stumper

Post by MachineCode »

Arctic Fox wrote:What if the assigned id value for username is larger than website?
:shock: I'm having some shocking seniors moments tonight. Forget I ever posted.
Kuron wrote:I can't imagine having to remember (and give) unique names for hundreds of gadgets.
How is remembering a global var name like "website" any different to remembering a enumerated constant like "#website" ?
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Kuron
Addict
Addict
Posts: 1626
Joined: Sat Oct 17, 2009 10:51 pm
Location: Pacific Northwest

Re: Newbie stumper

Post by Kuron »

MachineCode wrote:How is remembering a global var name like "website" any different to remembering a enumerated constant like "#website" ?
Your way would be great for small programs. It wouldn't work well for me for larger projects. There are many ways to skin a cat.
Best wishes to the PB community. Thank you for the memories. ♥️
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6175
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Newbie stumper

Post by blueznl »

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply