Page 1 of 1

Reusing (cloning) controls

Posted: Fri Oct 04, 2013 8:50 pm
by jalz
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

Re: Reusing (cloning) controls

Posted: Fri Oct 04, 2013 9:01 pm
by Simon
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

Re: Reusing (cloning) controls

Posted: Fri Oct 04, 2013 9:44 pm
by dunbarx
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

Re: Reusing (cloning) controls

Posted: Sat Oct 05, 2013 12:43 am
by jalz
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.

Re: Reusing (cloning) controls

Posted: Sat Oct 05, 2013 1:03 am
by Simon
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

Re: Reusing (cloning) controls

Posted: Sat Oct 05, 2013 3:11 pm
by jalz
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

Re: Reusing (cloning) controls

Posted: Sat Oct 05, 2013 3:20 pm
by Klaus
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

Re: Reusing (cloning) controls

Posted: Sat Oct 05, 2013 4:49 pm
by Simon
Yeah...
...long id...
Just add that and you should be happy :)

Simon

Re: Reusing (cloning) controls

Posted: Sun Oct 06, 2013 5:47 pm
by jalz
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.

Re: Reusing (cloning) controls

Posted: Sun Oct 06, 2013 5:54 pm
by SparkOut
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.

Re: Reusing (cloning) controls

Posted: Sun Oct 06, 2013 6:12 pm
by Simon
Please find an example attached.

Simon

Re: Reusing (cloning) controls

Posted: Sun Oct 06, 2013 6:35 pm
by Klaus
SparkOut wrote:There was a typo in Klaus' line with an extra "of" before the last id number.
Ouch, yes, sorry :oops:

Re: Reusing (cloning) controls

Posted: Sun Oct 06, 2013 11:39 pm
by jalz
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

Re: Reusing (cloning) controls

Posted: Mon Oct 07, 2013 8:17 am
by SparkOut
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!