Page 1 of 1

Handler for object created dynamically

Posted: Fri May 02, 2014 6:56 pm
by michel_bujardet
With a control placed on a card at design time, I simply go to the script for that control, but a dynamically created object does not exist at design time.

When I create a field dynamically, if the user clicks on that field, how and where can I create a handler for the mouseDown event for that object ?
Is that possible ?

Thanks.

Re: Handler for object created dynamically

Posted: Fri May 02, 2014 7:59 pm
by Simon
Hi Mich,
In this case you would use "target" and the mouseUp/down handler would be in the stack/card/group script.
on mouseUp
answer the target
end mouseUp

Simon (err one persons opinion)

Re: Handler for object created dynamically

Posted: Fri May 02, 2014 8:09 pm
by Dixie
To try this out... put this into a button on a card.. then 'click' it...:-)
Read up on 'template' in the Dictionary...

Code: Select all

on mouseUp
   set the width of the templatefield to 200
   set the height of the templatefield to 30
   set the textfont of the templatefield to "helvetica"
   set the textsize of the templatefield to 18
   set the loc of the templatefield to the loc of this card
   set the name of the templatefield to "myNewField"
   set the text of the templatefield to "'Click' me"
   set the lockText of the templatefield to true
   set the lockLoc of the templatefield to true
   put "on mouseDown" & cr & "beep" & cr & "end mouseDown" into theScript
   set the script of the templatefield to theScript
   create field
end mouseUp
or maybe something as simple as...

Code: Select all

   put "on mouseDown" & cr & "beep" & cr & "end mouseDown" into theScript
   set the script of button X of this card to theScript

Re: Handler for object created dynamically

Posted: Fri May 02, 2014 9:05 pm
by Simon
Hi Dixie,
Is it true that scriptLimits have been removed?

Simon

Re: Handler for object created dynamically

Posted: Fri May 02, 2014 11:01 pm
by michel_bujardet
Dixie wrote:To try this out... put this into a button on a card.. then 'click' it...:-)
Read up on 'template' in the Dictionary...

or maybe something as simple as...

Code: Select all

   put "on mouseDown" & cr & "beep" & cr & "end mouseDown" into theScript
   set the script of button X of this card to theScript
Dixie, I applied this and it worked like a charm. Thank you so much for sharing your terrific knowledge :)

Re: Handler for object created dynamically

Posted: Fri May 02, 2014 11:05 pm
by FourthWorld
A bug had prevented scriptLimits from working in some recent versions; this is planned for correction in v6.7:
http://quality.runrev.com/show_bug.cgi?id=11797

Re: Handler for object created dynamically

Posted: Sat May 03, 2014 1:25 am
by FourthWorld
One of the problems with LiveCode is that there are too many ways to do nearly everything, and all of them are fun to explore. :)

Another solution for this is to use behaviors: define the behavior for the newly-created fields in a separate object, and set the new field's behavior property to that object.

And if you prefer to create your template field graphically rather than in script, you could store one set up as you like it and use the copy command so you'll get your new field in one line:

Code: Select all

copy field "MyTemplateField" of stack "MyAppResources" to this card of the topstack

Re: Handler for object created dynamically

Posted: Sat May 03, 2014 8:59 am
by Dixie
Simon...

I thought that script limits had been removed... it seems there are no script limits in 'community' but there are in 'commercial'. below is a note to Ben Rubenstein from Mark Waddingham in the Quality Control Centre... Who knows which way it will pan out...:-)
Hi Ben,
The script limits were removed from community as they made no sense there - commercial was always intended to remain as it was.
We are currently reviewing the scriptlimits in commercial. In the meantime they are enforced in the 6.7+ engine.
Warmest Regards,
Mark.
Dixie

Re: Handler for object created dynamically

Posted: Sat May 03, 2014 9:00 am
by michel_bujardet
FourthWorld wrote:One of the problems with LiveCode is that there are too many ways to do nearly everything, and all of them are fun to explore. :)

Another solution for this is to use behaviors: define the behavior for the newly-created fields in a separate object, and set the new field's behavior property to that object.

And if you prefer to create your template field graphically rather than in script, you could store one set up as you like it and use the copy command so you'll get your new field in one line:

Code: Select all

copy field "MyTemplateField" of stack "MyAppResources" to this card of the topstack
Thank you for the copy command :)