AS3 Textbox Change Event Not Firing

25,809

Solution 1

Yes, as David comments, TextField only dispatches CHANGE events when it's being edited by user input - in fact, this is generally true of most components. The reason, of course, is because if you manually tried to change the field's value inside your "change" handler, you'd make a recursive cycle of events that you'd have to manually detect your way out of.

What you want to do is make a wrapper function, like:

function setQuestionText( s:String ):void
    {
        question_txt.text = s;
        setTextBubbleSize();
    }

For your other question, there is no reason to estimate the textfield's height based on the number of lines or characters - just set the text property, and you can immediately query the textfield's height. It will accurately reflect the field's new height, and if you change the field's width, the height will immediately be updated, etc.

Or, if you need to know any fine details about the size of the text field, you can always use TextExtent.

Solution 2

I don't think the CHANGE event is fired when you change the textbox's properly with as3. But you can always raise the event by:

dispatchEvent(new Event(Event.CHANGE, true));

For the number of lines in the textbox, you can use the numLines properly of the TextField

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html#numLines

Solution 3

As I see the solutions, this is another class that extends the TextField. Sample code:

package {
import flash.events.Event;
import flash.text.TextField;

public class MyTf extends TextField {
    public function MyTf() {
        super();
    }

    override public function set text(value:String):void
    {
        super.text = value;
        dispatchEvent(new Event(Event.CHANGE));
    }

}
}
Share:
25,809
Bryan
Author by

Bryan

Updated on July 09, 2022

Comments

  • Bryan
    Bryan almost 2 years

    I built a quiz game with a cartoon question bubble. The bubble is re sized to the length of the question. I want to a change event on the dynamic textbox to call a function that changes the size of the question bubble.

    However, the change event is never called when my textbox value is modified dynamically from code.

     question_txt.addEventListener(Event.CHANGE, setTextBubbleSize);
    
     function setTextBubbleSize(event:Event):void
        {
            trace("QUESTION TEXT CHANGED");
            textBubble_mc.height = 30 + question_txt.text.length * 1.2;
            if (textBubble_mc.height > 170) textBubble_mc.height = 170;
            question_txt.y = textBubble_mc.y - textBubble_mc.height / 6 + 10;
        }
    

    I want to use the change event because there are several places in my code that question_txt can be set. How can I get the textbox to fire the change event?

    Also, is there a way to count the number of lines in question_txt to more accurately set the height of textBubble_mc?

  • Bryan
    Bryan almost 15 years
    Thanks a lot David. I totally forgot about the numLines property.
  • Bryan
    Bryan almost 15 years
    I ended up just calling a function as you indicated in your sample code. For the second question, I would like to use the textfield's height property, but it does not resize automatically. I have a dynamic text box on the stage and if the text box is too small text gets cut off. I dont see anything in flash to make the textbox resizable. I will check the as3 library. Do you know how to make a textbox resizable to the length of the text.
  • Bryan
    Bryan almost 15 years
    I got it. question_txt.autoSize = TextFieldAutoSize.CENTER; Thanks fenomas!
  • Triynko
    Triynko about 14 years
    This is nice, but you can leverage existing Event.CHANGE handlers by calling question_txt.dispatchEvent( new Event( Event.CHANGE ) );
  • fenomas
    fenomas about 14 years
    Triynko: Good point! But if you later edit the handler code to use the event object's target property (or whatever), it will cause problems. If you want to future-proof it you'll need to make a real event object.
  • Triynko
    Triynko about 12 years
    fenomas, calling dispatchEvent, as I suggested, does dispatch a real event. Perhaps you misinterpreted my comment as suggesting to directly call the handlers, but that's not what I said... I said to leverage them (indirectly) by calling dispatchEvent, which generates a real, bubbling event, with the target property correctly set to the TextField instance, since it's dispatched directly from it, and the event will even bubble, updating the currentTarget property as it goes up their hierarchy.