4 Replies Latest reply on Aug 19, 2009 5:52 PM by Gregory Lafrance

    Event handler for a text field

    SiHoop Level 1

      How do I write an event handler that will be triggered when the value of a text field changes? I assume I can place an event in place of SOME EVENT HERE in the example below.

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>
              <![CDATA[
                 
                  private var newVal:Number=1
                  public function doThis():void{
                      newVal+=1
                      goal1.text=String(newVal)
                      //goal1.addEventListener(SOME EVENT HERE, textChange)
                  }
                  public function textChange():void{
                      trace("text changed")
                  }
              ]]>
          </mx:Script>
          <mx:Text id="goal1" color="#57782f" fontSize="30" text="1"  x="106" y="110"/>
          <mx:Button x="88" y="169" label="Button" click="doThis()"/>
      </mx:Application>

        • 1. Re: Event handler for a text field
          Gregory Lafrance Level 6

          "Text" does not dispathc a change event, so use a TextArea tweaked as follows.

           

          If this post answered your question or helped, please mark it as such.

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
              <mx:Script>
                  <![CDATA[           
                      private var newVal:Number=1
                      public function doThis():void{
                          newVal+=1
                          goal1.text=String(newVal)
                          goal1.addEventListener(Event.CHANGE, textChange)
                      }
                      public function textChange():void{
                          trace("text changed")
                      }
                  ]]>
              </mx:Script>
              <mx:TextArea id="goal1" color="#57782f" fontSize="30" text="1"  x="106" y="110"
               focusThickness="0" borderThickness="0" backgroundAlpha="0"/>
              <mx:Button x="88" y="169" label="Button" click="doThis()"/>
          </mx:Application>

          • 2. Re: Event handler for a text field
            SiHoop Level 1

            I tried the code you posted, but the listener did not work. Would you please check the code you gave me to see if it works for you?

            Thanks.

            • 3. Re: Event handler for a text field
              SiHoop Level 1

              The event you gave me: goal1.addEventListener(Event.CHANGE, textChange)

               

              only works for user changed data. For programmatically changed data I had to use valueCommit:

              goal1.addEventListener("valueCommit", textChange)

               

              Thank you for your help.

              • 4. Re: Event handler for a text field
                Gregory Lafrance Level 6

                That makes sense.