views:

60

answers:

3

Can I somehow find out what was the change in the textfield? I would want to compare the old text with the new text ... the problem is, that I have multiple textAreas in a tab-editor, and all the textAreas are watched by one eventListener. I want to get a value calculated by the next formula:

globalChangeCount += thisTextArea.currentCharacterCount - thisTextArea.oldtCharacterCount

where the globalChangeCount is a value modified by all changes in any of the textAreas.

I am searching for these values through the event variable, but can't seam to find the old text of the textArea.

A: 

You can use event.currentTarget to get a reference to the TextArea that fired the event, and use the focusIn event to execute a function to populate a variable with the old text value.

houser2112
I know I can get the textArea's variables through the event.currentTarget, but I didn't get it, what's with the focusIn ... the oldText is not the text what was before the textArea got focus, but its the text right before the change.
Biroka
If focusIn is not late enough for you, I think you have to listen for both keyDown (for keypresses that actually input text, and for ctrl+c and its equivalents) and click (in case of copying via context menu) events, and have those event listeners point to the same function to implement your logic.
houser2112
A: 

Maybe you should just subclass the TextArea and create an oldText field variable you update internally after all the external listeners have been notified.

Theo.T
How to update this oldText variable, because if I catch the event inside with Event.CHANGE I won't get the old velue. I don't want to watch KeyboardEvent.KEY_DOWN or similar, because text can be inserted without keypress too (copy/paste). How can I cath an event (or something) right before the Event.CHANGE? (after every keydown and copy/paste too)
Biroka
+1  A: 

This may or may not be what you're looking to do:

package
{
    import mx.controls.TextArea;


    public class CountingTextArea extends TextArea
    {

        public var staleText : String = "";

        [Bindable("textChanged")]
        [NonCommittingChangeEvent("change")]
        public function get charDiff() : int
        {
            var diff : int = staleText.length - text.length;
            staleText = text;
            return diff;
        }

        public function CountingTextArea()
        {
            super();
        }
    }
}

I made it so that you can use it as a source for binding. Instead of subscribing to the event on each TextArea, you can use:

function addWatchers():void
{
    ChangeWatcher.watch(countingTextArea1, ["charDiff"], charDiffChangeHandler );
    ...
    ChangeWatcher.watch(countingTextArea5, ["charDiff"], charDiffChangeHandler );
}

With the event handler somewhere too:

function charDiffChangeHandler( event : PropertyChangeEvent ) : void
{
    trace(event.currentTarget.charDiff);
    // or
    trace(event.newValue);
}
jooks
I didn't work with flags or what are those above the function, but it seams to be a solution for me.... how do I call this function, or does it automatically do this for me, and the only thing is to get that staleText when I catch the change event from outside the class ex. event.currentTarget.staleText?
Biroka
See updated above. I'm not sure how notification works here yet.
jooks