Page 1 of 1
Creating a Button from Code
Posted: Thu Jun 07, 2012 4:13 pm
by townsend
I'd like to be able to create some buttons with code.
I know it can be done like this:
Code: Select all
on mouseUp
create button "click me"
set the loc of it to 100,100
set the width of it to 50
set the height of it to 20
end mouseUp
But what about Handlers?
How do I define the events for buttons created with code?
Is there some way to have LC create coded handlers from code?
Or, do I have to create a static button with the proper handlers and then
copy or assign the handlers from my static button to the new dynamic one?
Re: Creating a Button from Code
Posted: Thu Jun 07, 2012 4:24 pm
by Dixie
Hi...
Something like this ?
Code: Select all
on mouseUp
set the script of the templateButton to "on mouseUp" & cr & "beep" & cr & "end mouseUp"
set the label of the templateButton to "I'm New"
create button
end mouseUp
be well
Dixie
Re: Creating a Button from Code
Posted: Thu Jun 07, 2012 4:38 pm
by Klaus
Hi townsend (Pete?

)
yep, just like Dixie says, that will work for smaller code snipptes, but not more than 10 statements
due to the LiveCode "scriptlimits"! Look this up in the dictionary.
If you need longer scripts, you can always attach a (premade) behavior to newly created objects!
Best
Klaus
Re: Creating a Button from Code
Posted: Thu Jun 07, 2012 5:10 pm
by mwieder
Klaus-
If you're staying in the IDE and not trying to do this from a standalone app then the scriptlimit doesn't apply.
Re: Creating a Button from Code
Posted: Thu Jun 07, 2012 5:21 pm
by townsend
Okay-- It works. Pretty slick. Thanks Dixie!
This means LiveCode can write and then execute code on the fly.
Not many languages can do that!
Note: I looked up the
templatebutton reserved word.
It's used if you want all the dynamic buttons to be the same.
With the code below, they can all be different.
Code: Select all
on mouseUp
put "click me" into temp
create button temp
set the loc of it to 100,100
set the width of it to 50
set the height of it to 20
set the script of btn "click me" to "on mouseUp" & cr & "beep" & cr & "end mouseUp"
set the label of btn "click me" to "I'm New"
end mouseUp
PS: Klaus-- were can I read more about these (premade) behaviors?
Re: Creating a Button from Code
Posted: Thu Jun 07, 2012 5:35 pm
by Klaus
Hi Townsend,
check the "User Guide" (Menu: Help)
I copied that part for our conveninece

###################################################
5.8.7 Writing reusable code using behaviors
Behaviors are a method to create common functionality between objects without duplicating the scripts.
An object with a behavior set will act as though its script was set to the script of the behavior button.
If multiple objects share the same behavior, each will have its own set of script local variables.
Any references to me, the owner of me, etc will resolve to the child object currently executing.
Behavior scripts must be stored in button objects. To set an object’s behavior, refer to the long id of the button containing the behavior script.
##################################################
Basically you create a button, give it a meaningful name and add as much scripts as you need.
'Then you can create new button(s) and just attach that buttons "behavior" to the new button:
Code: Select all
on mouseUp
put "click me" into temp
create button temp
set the loc of it to 100,100
...
set the behavior of it to the long ID of btn "your premade behavior button here" of cd X ## of stack Y
end mouseUp
Done!
This is a great way to avoid the "scriptlimits".
With clever use of custom properties you can even have more control, since you can attach different
custom properties to newly created objects and with something like "the cProperty of ME" you can even
have different actions with the same behavior. Know what I mean?
Best
Klaus
Re: Creating a Button from Code
Posted: Thu Jun 07, 2012 6:13 pm
by townsend
Thanks Klaus-- it good to know about behaviors.
Another way to get around script limits is to put core code into functions.
Then you can run a sequential set of functions to accomplish much more than 10 lines of code.
This is VERY powerful feature of LiveCode-- and opens up MANY new possibilities.
I may be wrong but I don't think this is allowed in traditional languages.
I'm surprised this isn't touted more, as a unique LiveCode benefit.
That is-- the ability of code, to write and run code, without having to recompile.
Re: Creating a Button from Code
Posted: Thu Jun 07, 2012 6:22 pm
by mwieder
Well, maybe it depends on how you define "traditional", but ruby supports this brilliantly through the "yield" statement. And lisp does something similar with the anonymous lambda function.
But yes, they all support the separation of *what* the code does from *when* and *where* it does it. And it's a really powerful concept.
Re: Creating a Button from Code
Posted: Wed Jun 13, 2012 8:41 pm
by townsend
I'm having a problem creating objects across stack boundaries.
I've tried different combinations of: in, on, and of.
Nothing seems to work. For instance.
Code: Select all
create button "test" in card "one" of stack "Look"
Re: Creating a Button from Code
Posted: Wed Jun 13, 2012 8:52 pm
by Klaus
Hi townsend,
well, this is correct behavior!
"create" will only accept "... in grp X" as a parameter, but unfortunately NOT something like "... on cd y of stack z"
You could try to go to the card first, maybe with "lock messages" and "lock screen" and then
go back where you came from.
Best
Klaus
Re: Creating a Button from Code
Posted: Wed Jun 13, 2012 9:47 pm
by mwieder
You could try to go to the card first, maybe with "lock messages" and "lock screen" and then
go back where you came from.
... and if you're going to do that, check out the "push card" and "pop card" commands.
Re: Creating a Button from Code
Posted: Thu Jun 14, 2012 9:17 am
by Klaus
Hi Mark,
mwieder wrote:klaus wrote:...and then go back where you came from.
... and if you're going to do that, check out the "push card" and "pop card" commands.
hm, I don't have the slightest idea, how these command might help him find his way back to Seattle!?
Sorry, never can resist...

Re: Creating a Button from Code
Posted: Thu Jun 14, 2012 5:29 pm
by mwieder
Hard to say... I haven't found my way back to Seattle in a bit over a decade.
