Create a handler in a control programmatically.

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
djkesler
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 21
Joined: Tue Jun 01, 2010 10:37 pm

Create a handler in a control programmatically.

Post by djkesler » Sat Nov 10, 2018 11:30 pm

Is there a way to create a handler script in a newly created field, button, or group, programmatically?

I want to put a few handler scripts in a control that I am programmatically creating.

Or, programmatically, can handler scripts be copied from a control and pasted into a control?

Thanks for any help.

David

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6173
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Create a handler in a control programmatically.

Post by FourthWorld » Sat Nov 10, 2018 11:49 pm

The script of an object is a property, so common property assignment syntax will do what you need:

Code: Select all

set the script of btn 1 to tSomeVariableContainingAScript
That said, dynamic code is notoriously hard to debug and frequently a disproportionate source of problems during long-term maintenance of a code base.

If you tell us a bit about what you want to accomplish we may be able to find a simpler solution.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

djkesler
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 21
Joined: Tue Jun 01, 2010 10:37 pm

Re: Create a handler in a control programmatically.

Post by djkesler » Sun Nov 11, 2018 12:28 am

Thanks for the insight.

I am wanting to provide some simple handlers at the group level for controls I create from db table info. They are very simple closefield, menupick, etc. for controls located in the group.

Thanks again. I am working on a presentation for LCGobal on “Arrays to MySQL and back” on Thursday.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6173
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Create a handler in a control programmatically.

Post by FourthWorld » Sun Nov 11, 2018 12:42 am

Would behaviors do the trick? Then you have code in one place, easily assignable to any number of objects as needed.

http://lessons.livecode.com/m/4071/c/16766
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5569
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Create a handler in a control programmatically.

Post by dunbarx » Sun Nov 11, 2018 4:28 am

There is a concept know as "self modifying code". This is where the code itself is changed while executing to accommodate the programmer's intentions. Livecode will have trouble with this because it does not at all like having a "script" property modified while a handler in that script is running .

I have played around with setting the script of an object from the outside, but have never had to do so to implement any process I ever worked on. If there is a ""code change" required, it should always be possible to simply execute a pre-defined code snippet, separated in, say, a conditional or a switch construction.

In other words, say that "something" engenders "somethingElse", and "this" engenders "that". Those two "methods" should not require a change in the code of a handler, rather only forks in that handler. Further, whatever triggers either situation can also be handled in the, er, handler.

Craig Newman
Last edited by dunbarx on Sun Nov 11, 2018 3:22 pm, edited 1 time in total.

djkesler
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 21
Joined: Tue Jun 01, 2010 10:37 pm

Re: Create a handler in a control programmatically.

Post by djkesler » Sun Nov 11, 2018 5:04 am

Self modifying code is way over my head, and pay grade!

But the behavior idea would work. Since the long ID of the button is specified, it probably doesn’t need to be in the message path, right? I would want the code to work on any card in the stack. As I understand it, if the behavior is tied to a number of groups the phrase “of me” would make reference to the group that was executing the behavior as if the code was located in the group script. Is that correct?

Thanks for the help

David

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2247
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Create a handler in a control programmatically.

Post by jmburnod » Sun Nov 11, 2018 10:25 am

What Dictionary about "me" is not clear for me (LC 9.0.2 rc 1)
put me into myVariable -- puts name, if me is not a container
MyVariable = empty if the target is a button, but

Code: Select all

put the short name of me

works
put me into myVariable -- puts contents, if me is a container
Works with groups, flds and images (with behavior or not)
I tested:
For group: return the short name of group
For Fields: return the text of fld
For images: return the short name of image

Best regards
Jean-Marc

Post Reply

Return to “Talking LiveCode”