Reusing (cloning) controls

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
jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Reusing (cloning) controls

Post by jalz » Fri Oct 04, 2013 8:50 pm

Hi,

I've created a control on a substack which seems to do everything I want. The control itself has alot of code attached to it. I can copy and paste the control on the other cards I have which is no problem but I was wondering whether I could treat it as a master control and then clone it across the various cards/stacks I have so if for whatever reason I need to change/edit code, I'd like to edit it on the master control and it would filter through to all the clone of that control. Is that possible with controls?

I know you can do this cloning with images, so i'm hoping you can do it with controls but I cant seem to figure it out.

Thanks
Jalz

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Reusing (cloning) controls

Post by Simon » Fri Oct 04, 2013 9:01 pm

Hi Jalz,
liveCode is very cool!
You should check out "behaviors" here is a short lesson:
http://lessons.runrev.com/s/lessons/m/4 ... a-behavior

So, you have your master button, get it's long id and paste that into the behavior (it's the last field in the button inspector) of the sub buttons :)
Sweet!

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Reusing (cloning) controls

Post by dunbarx » Fri Oct 04, 2013 9:44 pm

Simon has pointed out an excellent way, as long as your master control is a button. Behaviors (master) are not supported by any other object type.

The advantage here is that a master button can contain many scripts, all of which form a new custom message path that can include any other control of any kind anywhere. That message path is inclusive for any object with its behavior properly set to the master button. This has fabulous potential.

The other way to do this is to place those "library" handlers in a backscript, which is also accessible to all open stacks and their controls and cards, and are available at ubiquitously without having to create explicit links, as I said. If you place a command or function call in any script anywhere, it will be trapped in the backscript.

Each method has its advantages and disadvantages. For example, a disadvantage of the backscript method is that you may forget to pass commonly used handlers that may have been trapped along the hierarchy, and so they may not make it all the way to the library script. A disadvantage of the behavior method is that you must explicitly set the property for each control to the long ID of the master, including any new ones that might be created.

The point is to know how both work. They provide enormous power and flexibility.

Craig Newman

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: Reusing (cloning) controls

Post by jalz » Sat Oct 05, 2013 12:43 am

Hi Simon and Craig,

Thank you for replying. I've been looking at the behaviours method, looks like the best method for what I need. The only thing is, on my controls stack I've got the master combobox button on there, and on my regular stack when I insert a blank button and give it the behaviour of my master button (which works), but I cant seem to change the blank button into a combobox.

When I insert a standard combobox on my stack, I cant seem to find behaviours so I can reference my master control. Is there something that I have overlooked?

Thanks once again.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Reusing (cloning) controls

Post by Simon » Sat Oct 05, 2013 1:03 am

Ok got this one :)

You have to set it in code:
set the behavior of btn 2 to the long id of btn 1

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: Reusing (cloning) controls

Post by jalz » Sat Oct 05, 2013 3:11 pm

Hi Simon,

Thanks - I just cant get it to work for some reason.

I'll tell you what I've done. I've got a substack called Controls, on that sbstack I have my master combobox control with the code associated to it. The control has an id of 1004

On my app stack, I've copied across a combobox (which has no code associated to it) - id 46610. I've then got a script which I'm manually running to try see if I can reference button id 1004.

i've tried the following line of code but not had much luck.

set the behavior of btn 46610 to the btn id of 1004

set the behavior of button id 46610 to button id 1004

set the behavior of button id 46610 to the button id 1004 of stack "Controls"

set the behavior of button id 46610 to button id 1004 of stack "Controls"

What am I missing out, I've checked the documentation tab for for the behaviour command and it suggests
set the behavior of childObject to behaviorObjectReference - which I think I'm doing?

Thanks

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

Re: Reusing (cloning) controls

Post by Klaus » Sat Oct 05, 2013 3:20 pm

Hi jalz,

Simon's one-liner showed the correct syntax:
...
set the behavior of btn 2 to the long id of btn 1
...

Just use it and all will be fine:
...
set the behavior of btn 46610 to THE LONG ID OF btn id of 1004
## You really should use meaningful object names! 8)
...

Hint:
If in doubt, check the dictionary for correct syntax!


Best

Klaus

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Reusing (cloning) controls

Post by Simon » Sat Oct 05, 2013 4:49 pm

Yeah...
...long id...
Just add that and you should be happy :)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: Reusing (cloning) controls

Post by jalz » Sun Oct 06, 2013 5:47 pm

Thanks Guys,

Just cant get it to work, seem to be getting a bad factor error, no matter what I try. I've created a sample stack with the code you've sent, is there something I'm doing that I shouldn't be doing thats obvious?

Apologies, I've gone through so many variations trying to get it work, I know it must be something simple.
Attachments
behaviortest.livecode.zip
my test file
(1.3 KiB) Downloaded 240 times

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Reusing (cloning) controls

Post by SparkOut » Sun Oct 06, 2013 5:54 pm

There was a typo in Klaus' line with an extra "of" before the last id number.

The main problem is with you using the id number, you have to make sure you are giving the right reference to the control. In the case of the downloaded stack, the master button id I see is 1138. So your line should read:

Code: Select all

set the behavior of btn id 1136 to THE LONG ID OF btn id 1138
Careful reference to the controls you are using as master and copies is needed to make sure you are giving the right id in each case.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Reusing (cloning) controls

Post by Simon » Sun Oct 06, 2013 6:12 pm

Please find an example attached.

Simon
Attachments
Behavior.zip
LC 6.1
(883 Bytes) Downloaded 232 times
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Reusing (cloning) controls

Post by Klaus » Sun Oct 06, 2013 6:35 pm

SparkOut wrote:There was a typo in Klaus' line with an extra "of" before the last id number.
Ouch, yes, sorry :oops:

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: Reusing (cloning) controls

Post by jalz » Sun Oct 06, 2013 11:39 pm

Just wanted to thankyou all for taking the time to help me out. Apologies for the constant questions, just trying to get my head around the syntax - I come from FileMaker.

With you help I've got the behaviors now passing through script - so I can get on with creating my UI :)

All the best
Jalz

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Reusing (cloning) controls

Post by SparkOut » Mon Oct 07, 2013 8:17 am

Never apologise for asking questions here! Questions help you (and the answerer and all the readers) gain information about how to work better with LiveCode. As you aren't saying "here's something you have to do for me" then you'll find nobody around here is selfish with either code or information. Keep trying, keep asking!

Post Reply