Attaching string as GadgetData

Just starting out? Need help? Post your questions and find answers here.
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Attaching string as GadgetData

Post by kpeters58 »

Do I have to attach a string pointer to Gadgets seeing that the Get/SetGadgetData value parameter only accepts integers or is there a simpler way I am missing?
PB 5.73 on Windows 10 & OS X High Sierra
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Attaching string as GadgetData

Post by Fangbeast »

G'day, I asked about this ages ago and the answer was yes. By using pointers, you can even attach entire structures to gadgets that way.

Mind you, I still don't quite know how to use this.
Amateur Radio, D-STAR/VK3HAF
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Attaching string as GadgetData

Post by Thunder93 »

Yep. The help file gives you one approach using arrays. But you probably prefer the following;

Code: Select all

If OpenWindow(0, 0, 0, 190, 100, "SetGadgetData", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ButtonGadget(0,  10, 10, 80, 20, "Button"): SetGadgetData(0, @"Good morning")
  ButtonGadget(1,  10, 40, 80, 20, "Button"): SetGadgetData(1, @"Hello World")
  ButtonGadget(2,  10, 70, 80, 20, "Button"): SetGadgetData(2, @"Nothing to say")
  ButtonGadget(3, 100, 10, 80, 20, "Button"): SetGadgetData(3, @"Nothing to say")
  ButtonGadget(4, 100, 40, 80, 20, "Button") 
  ButtonGadget(5, 100, 70, 80, 20, "Button")
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_Gadget
      Value = GetGadgetData(EventGadget())
      If Value
        Debug PeekS(Value)
      EndIf
    EndIf
  Until Event = #PB_Event_CloseWindow
EndIf
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Attaching string as GadgetData

Post by Fangbeast »

Code: Select all

simply this (only demonstration)

I went searching anf found a good example from Bisonte (to me) about simple use of structure if you needed more than a string pointer.
[code]
Structure struct
  Text.s
  Integer.i
EndStructure

*Pointer.struct = AllocateMemory(SizeOf(struct))

If *Pointer
  *Pointer\Text = "Hello"
  SetGadgetData(#Gadget, *Pointer) ; Or SetGadgetItemData(#Gadget, Item, *Pointer)
  [...]
  *MyPointer.struct = GetGadgetData(#Gadget) ; or GetGadgetItem(#Gadget, Item)
  Debug *MyPointer\Text
EndIf
Amateur Radio, D-STAR/VK3HAF
User avatar
Bisonte
Addict
Addict
Posts: 1232
Joined: Tue Oct 09, 2007 2:15 am

Re: Attaching string as GadgetData

Post by Bisonte »

Or without something of "pointermagic"

Code: Select all

Global NewMap mGadgetText.s()

Procedure.i SetGadgetDataText(Gadget, Text.s)
  
  Protected Result = #False, Key.s = Hex(Gadget)
  
  If IsGadget(Gadget)
    mGadgetText(Key) = Text
    Result = #True
  EndIf
  
  ProcedureReturn Result
  
EndProcedure
Procedure.s GetGadgetDataText(Gadget)
  
  Protected Key.s = Hex(Gadget)
  Protected Result.s = ""
  
  If FindMapElement(mGadgetText(), Key)
    Result = mGadgetText(Key)  
  EndIf
  
  ProcedureReturn Result
  
EndProcedure
Procedure.i RemoveGadgetDataText(Gadget)
  
  Protected Key.s = Hex(Gadget)
  Protected Result = #False
  
  If FindMapElement(mGadgetText(), Key)
    DeleteMapElement(mGadgetText(), Key)
    Result = #True
  EndIf 
  
  ProcedureReturn Result
  
EndProcedure
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
mk-soft
Always Here
Always Here
Posts: 5393
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Attaching string as GadgetData

Post by mk-soft »

@Bisonte :wink:
very nice

I have this Modul. Named GadgetData http://www.purebasic.fr/english/viewtop ... 12&t=63937
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply