Creating a Button from Code

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Creating a Button from Code

Post by townsend » Thu Jun 07, 2012 4:13 pm

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?

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Creating a Button from Code

Post by Dixie » Thu Jun 07, 2012 4:24 pm

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

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Creating a Button from Code

Post by Klaus » Thu Jun 07, 2012 4:38 pm

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Creating a Button from Code

Post by mwieder » Thu Jun 07, 2012 5:10 pm

Klaus-

If you're staying in the IDE and not trying to do this from a standalone app then the scriptlimit doesn't apply.

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: Creating a Button from Code

Post by townsend » Thu Jun 07, 2012 5:21 pm

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?

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Creating a Button from Code

Post by Klaus » Thu Jun 07, 2012 5:35 pm

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

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: Creating a Button from Code

Post by townsend » Thu Jun 07, 2012 6:13 pm

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.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Creating a Button from Code

Post by mwieder » Thu Jun 07, 2012 6:22 pm

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.

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: Creating a Button from Code

Post by townsend » Wed Jun 13, 2012 8:41 pm

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"

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Creating a Button from Code

Post by Klaus » Wed Jun 13, 2012 8:52 pm

Hi townsend,

well, this is correct behavior! 8)
"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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Creating a Button from Code

Post by mwieder » Wed Jun 13, 2012 9:47 pm

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.

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Creating a Button from Code

Post by Klaus » Thu Jun 14, 2012 9:17 am

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... :lol: :lol: :lol:

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Creating a Button from Code

Post by mwieder » Thu Jun 14, 2012 5:29 pm

Hard to say... I haven't found my way back to Seattle in a bit over a decade. :P

Post Reply