Create a handler in a control programmatically.
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Create a handler in a control programmatically.
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9834
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Create a handler in a control programmatically.
The script of an object is a property, so common property assignment syntax will do what you need:
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.
Code: Select all
set the script of btn 1 to tSomeVariableContainingAScript
If you tell us a bit about what you want to accomplish we may be able to find a simpler solution.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Create a handler in a control programmatically.
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 9834
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Create a handler in a control programmatically.
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
http://lessons.livecode.com/m/4071/c/16766
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 9662
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Create a handler in a control programmatically.
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
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.
Re: Create a handler in a control programmatically.
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
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
-
- VIP Livecode Opensource Backer
- Posts: 2718
- Joined: Sat Dec 22, 2007 5:35 pm
- Location: Genève
- Contact:
Re: Create a handler in a control programmatically.
What Dictionary about "me" is not clear for me (LC 9.0.2 rc 1)
works
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
MyVariable = empty if the target is a button, butput me into myVariable -- puts name, if me is not a container
Code: Select all
put the short name of me
works
Works with groups, flds and images (with behavior or not)put me into myVariable -- puts contents, if me is a container
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
https://alternatic.ch