Page 1 of 1

[Solved] Set/GetGadgetData as strings

Posted: Tue Mar 10, 2020 1:30 am
by BarryG
Can these be strings instead of numbers?

[Edit] Please ignore this request; see Little John's post below (it can already be done).

Re: Set/GetGadgetData as strings

Posted: Tue Mar 10, 2020 3:57 am
by Little John
BarryG wrote:Can these be strings instead of numbers? If I need to store numbers with it, I can just do Str(number) and Val(number$), so it makes sense to make them strings instead for greater flexibility.
No. Now there is greater flexibility, because SetGadgetData() can be used to store a pointer to a structure, to an allocated memory area or to anything else you want.

Re: Set/GetGadgetData as strings

Posted: Tue Mar 10, 2020 4:07 am
by BarryG
Good point, Little John!

Re: [Solved] Set/GetGadgetData as strings

Posted: Tue Mar 10, 2020 9:55 am
by mk-soft
For remember

Code: Select all

*pText.String = AllocateStructure(String)
*pText\s = "Hello World!"
Debug *pText\s
FreeStructure(*pText)

Re: [Solved] Set/GetGadgetData as strings

Posted: Wed Mar 11, 2020 11:41 am
by Kwai chang caine
Litthe John wrote:No. Now there is greater flexibility, because SetGadgetData() can be used to store a pointer to a structure, to an allocated memory area or to anything else you want.
I don't know that, again yesterday i needed this option :oops: thanks Little John 8)
Thanks at BarryG, for lightning this subject and MkSoft for writing his example, always usefull for me for understanding a great text explanation 8)

Code: Select all

OpenWindow(0, x, y, 300, 200, "String en gadgetdata")
ButtonGadget(0, 10, 10, 30, 20, "Data")

*pText.String = AllocateStructure(String)
*pText\s = "Hello World!"
SetGadgetData(0, *pText)
*pText = 0
*pText = GetGadgetData(0)
Debug *pText\s
FreeStructure(*pText)

Repeat : WaitWindowEvent() :Until GetAsyncKeyState_(#VK_ESCAPE)

Re: [Solved] Set/GetGadgetData as strings

Posted: Wed Mar 11, 2020 5:36 pm
by mk-soft
Kwai chang caine wrote:

Code: Select all

OpenWindow(0, x, y, 300, 200, "String en gadgetdata")
ButtonGadget(0, 10, 10, 30, 20, "Data")

*pText.String = AllocateStructure(String)
*pText\s = "Hello World!"
SetGadgetData(0, *pText)
*pText = 0
*pText = GetGadgetData(0)
Debug *pText\s
FreeStructure(*pText)

Repeat : WaitWindowEvent() :Until GetAsyncKeyState_(#VK_ESCAPE)
You forget remove pointer from gadget

Code: Select all

*pText = GetGadgetData(0)
SetGadgetData(0, 0)
FreeStructure(*pText)

Re: [Solved] Set/GetGadgetData as strings

Posted: Wed Mar 11, 2020 6:57 pm
by Kwai chang caine
Thanks MkSoft, i don't think to do that 8)

Re: [Solved] Set/GetGadgetData as strings

Posted: Wed Mar 11, 2020 7:16 pm
by mk-soft
To avoid memory leak...

Code: Select all

Procedure FreeGadgetWithData(Gadget)
  Protected *mem
  If IsGadget(Gadget)
    *mem = GetGadgetData(Gadget)
    If *mem
      FreeStructure(*mem)
    EndIf
    FreeGadget(Gadget)
  EndIf
EndProcedure