Page 1 of 1

field changed message

Posted: Wed Oct 17, 2012 11:11 pm
by adventuresofgreg
Hi: there is probably a simple way of doing this, but I can't seem to figure it out. I want a handler in the script of a field that does something when the content of the field has changed. I don't really want to set up a loop to check for a change - unless i have to I guess. The user does NOT change this field. It's contents are streamed from a port.

Thanks!
Greg

Re: field changed message

Posted: Wed Oct 17, 2012 11:42 pm
by sturgis
There are a couple options. There is the textchanged message that will trigger even with programmatic text changes to the field. Though I'm not sure its necessary because if you're reading from a port and putting new content into the field, you could manually call a handler right after you add the text to the field.

Either method should work.

Code: Select all

on textchanged
## do something
end textchanged

Re: field changed message

Posted: Thu Oct 18, 2012 12:28 am
by adventuresofgreg
Thanks. When I search for "textChanged" it is not found in the dictionary

Re: field changed message

Posted: Thu Oct 18, 2012 12:51 am
by sturgis
textChanged is a recent addition. What version of livecode are you running? There are some more recent versions where it is available but not yet in the dictionary, though I think the most recent does show textChanged.

Re: field changed message

Posted: Thu Oct 18, 2012 1:40 pm
by adventuresofgreg
5.5.1

Re: field changed message

Posted: Thu Oct 18, 2012 1:55 pm
by sturgis
Yep, it should be there. (though you can get 5.5.2 now)

To test, create a field and put the following in its script.

Code: Select all

on textChanged
  put the text of me
end textChanged
As you type in the field it should echo the contents to the message box. (the first char you type might shift focus to the msg box so you'll have to click back into the field)

Heres most of the 5.5.2 dictionary entry..
Summary:
Sent when the content of a field has changed.

Examples:
on textChanged -- enable the save button when a change is made
enable button "save"
end textChanged


Is dispatched by the field whenever a user (or simulated user) action causes the content of the field to change.

Comments:
Handle the textChanged message if you want to perform an action when the content of a field changes.

The message is sent immediately after the input operation completes, but before a screen update is requested. The corresponding update occurs at the end of the first command in the textChanged handler. This means that you can lock the screen at the first line of the handler to delay the screen update (allowing you to modify the content of the field without any flicker).

To prevent potential for infinite recursion, calls to textChanged do not nest. Once a textChanged handler is being executed for a given field, another textChanged message is not sent to it, should a subsequent one be triggered.

The textChanged message is sent after messages such as keyDown and pasteKey but before messages such as keyUp.

Re: field changed message

Posted: Sun Oct 21, 2012 2:29 pm
by adventuresofgreg
Hi: The TextChanged handler doesn't work in my case because I am changing the fields contents with a script, and not by TYPING into the field. This handler only works when the field has been entered, and then contents changed.

Re: field changed message

Posted: Sun Oct 21, 2012 3:54 pm
by timlit
I might do as follows:

Code: Select all

- put field id (i) into tHistory[i]
- put the_external_value into field[i]
- if tHistory[i] <> field id (i) then answer "the value has changed!"
in data packed systems, I generally find it helpful to pass gui-targeted values through arrays.

hth,

Re: field changed message

Posted: Sun Oct 21, 2012 4:10 pm
by sturgis
Interesting. I could have sworn one of the recent versions of lc had a textchanged that would respond to script changes.

And this line

Code: Select all

Is dispatched by the field whenever a user [b](or simulated user) action[/b] causes the content of the field to change.
seems to indicate that it still should.

However if you look at the earlier answers there is this..
Though I'm not sure its necessary because if you're reading from a port and putting new content into the field, you could manually call a handler right after you add the text to the field.
Meaning..

Code: Select all

on handlerthatchangesthefield
do stuff here
do more stuff
-- run the command for when the field contents change
myFieldChanged -- 
end handlerthatchangesthefield

command myFieldChanged
do whateve you need done when the field changes
end myFieldChanged

Not sure I see a problem here even though the textchanged doesn't work as expected.

Re: field changed message

Posted: Sun Oct 21, 2012 4:38 pm
by adventuresofgreg
Thanks - there isn't really a big problem with adding my text changed stuff to the handler that reads from the port and writes to the field. That works fine, and might be the fastest way of doing it. However, there *may* be times when the port isn't working (data server streaming to the port is down), and I may want to provide some other method of writing data to that field aside from reading from the port. In this case, my handler to act upon a change in the contents of the field (if delt with in the port writing handler) won't work, and would need to be duplicated - which is what I'm trying to avoid. But... I could just write a handler to handle it - or a function. And just call the function in the port handler - and the same functioon from any new thing I have to write.

Re: field changed message

Posted: Sun Oct 21, 2012 7:31 pm
by jacque
sturgis wrote:Interesting. I could have sworn one of the recent versions of lc had a textchanged that would respond to script changes.
I read it the same way but yeah, "putting" text into a field doesn't do it. Apparently "user simulated" changes means simulated typing. I see that the "type" command does trigger a textchanged message.

Interesting.

Re: field changed message

Posted: Tue Oct 27, 2015 9:28 am
by mrcoollion
Still does not work in version 7.06 if I enter a value in a field from a script. :(

I will solve it with a handler ...

Re: field changed message

Posted: Tue Oct 27, 2015 11:53 am
by bn
Still does not work in version 7.06 if I enter a value in a field from a script. :(
I will solve it with a handler ...
why don't you send "textChanged" after you field changing script is done?

Code: Select all

on mouseUp
   put "asldfkj" after field "input"
   send "textChanged" to field "input"
end mouseUp
Kind regards
Bernd

Re: field changed message

Posted: Tue Oct 27, 2015 6:14 pm
by mrcoollion
Thx Bernd :)