Page 1 of 1

[SOLVED] Creating and destroying cards/controls dynamically

Posted: Fri Jul 10, 2020 11:25 pm
by anmldr
I just learned how to create a new card while my app is running by using "new card". Simple.

I also created a new button on that card but it's properties are not necessarily as easy. In the Property Inspector, in the Size and Position tab, there is a button for "Fit Content". What is the syntax for the different styles and for the "Fit Content" to handle this in script? Better yet, you can point me to a LC lesson or other materials that would help because it isn't just buttons that I want to create on the go.

Although I wrote this code, I don't understand why the new button is not being created on the card that I am currently on but rather it is created on the newly created card. That is what I wanted but I don't understand the "why".

Code: Select all

on mouseUp
   new card "theNewCard"
   new button "theNewbutton"
   go to card "theNewCard"
end mouseUp
Oh yes, where is the best place to put code (and what would the code be?) for deleting the newly made card when I want to leave that card? I did find "deleteCard" in the Dictionary.

How do I set script for a newly created button on a newly created card?

Again, if you want to point me to a document that deals with this, it is OK.

Thank you,
Linda

Re: Creating and destroying cards/controls dynamically

Posted: Sat Jul 11, 2020 3:12 am
by dunbarx
Hi.

When you create a new card, either with the "new card" command or the "create card" command, that card becomes the "current" card. Try this:

Code: Select all

on mouseUp
   new card "theNewCard"
   answer the name of this Card
   create button "theNewbutton"
   --go to card "theNewCard" -- not needed
end mouseUp
The "answer" command stops execution, so you can see where you are.

All properties are open for you to modify, and if you are doing this under script control, you never use the inspector:

Code: Select all

  create button "theNewbutton"
  set the style of btn "theNewButton" to "shadow"
Check out the "templateButton" and all the other templates, to see how you might get a head start on creating controls that share common properties.

Fit content is generally used to set the rect of a control so it fully displays its contents, label, whatever.
where is the best place to put code (and what would the code be?) for deleting the newly made card
Well, you cannot invoke it on the card you want to delete. That is like lifting yourself up by your bootstraps. But anywhere else is fine.
How do I set script for a newly created button on a newly created card?
The script of an object is a property of that object, just like any other:

Code: Select all

set the script of btn "theNewButton" to yourScript
Practice, Linda, practice.

Craig

Re: Creating and destroying cards/controls dynamically

Posted: Sat Jul 11, 2020 5:52 am
by anmldr
Gracias.

Yes, practice...that is what I am doing. I am reading, learning, watching the old conferences, and most currently, Lockdown Learning. When I cannot find what I need, I don't hesitate to ask here.

Linda

Re: Creating and destroying cards/controls dynamically

Posted: Sat Jul 11, 2020 9:35 am
by SparkOut
Hi Linda,

Additionally you can check the formattedWidth (and formattedHeight) to determine how much space the content of an object should take up.
The "fit content" option in the PI can essentially be done in script by the command

Code: Select all

set the width of <the object> to the formattedWidth of <the object>
if you're doing this dynamically, then you should be aware it could cause an object to resize beyond the limits you expect.

And did you see what Craig meant about deleting the card you are on? You could send that message to a handler in the stack script in the closeCard script of the card, to be caught and handled after the card has closed.

Card script

Code: Select all

on closeCard
   send "deleteTheCard" && the short name of this card to this stack in 100 milliseconds
end closeCard
Stack script

Code: Select all

on deleteTheCard pCardName
   --do some error trapping too
   delete card pCardName
end deleteTheCard

Re: Creating and destroying cards/controls dynamically

Posted: Sat Jul 11, 2020 9:29 pm
by anmldr
Great. Thank you both.