5 Replies Latest reply on May 22, 2009 3:26 PM by jherreso

    Strange TextInput Bug

    jherreso

      I'm working on a text messaging system and I want to clear the TextInput after the user send the message. The message can be sent using the "send" button or by pressing the RETURN key when the "newMessage" TextInput has focus.

       

      This is the relevant code:

       

      MXML:

       

      <mx:TextInput id="newMessage" width="100%" keyDown="{messageKeyPressed(event);}" />
      <mx:Button id="send" label="Send" click="{sendMessage();}" />

       

      AS:

       

      private function sendMessage():void {
           _comm.sendMessage(newMessage.text);
           newMessage.text = "";
      }

      private function messageKeyPressed(event:KeyboardEvent):void {
           if(event.keyCode == Keyboard.ENTER) sendMessage();
      }

       

       

      The weird thing is that if the message is sent using the "send" button the newMessage text gets cleared, but if the message is sent using the RETURN key, the message is sent (I receive it in the other end of the app) but the "newMessage.text" remains with the text entered.

       

      If I debug it, in the Variables window I see that the text property of the newMessage object is cleared, but the actual object remains with the same text.

       

      I guessed it had something to do with the focus, so I tried changing the focus to another object before clearing the newMessage.text. It works if I don't change the focus back to the TextInput object, but if I change the focus back to the "newMessage" TextInput, it doesn't clear the text (although it gets highlighted). I could just change to focus to another object, but that's not good from the usability point of view.

       

      Any ideas?

        • 1. Re: Strange TextInput Bug
          Gregory Lafrance Level 6

          I little redundant, but try this:

           

          private function sendMessage():void {
               _comm.sendMessage(newMessage.text);
               newMessage.text = "";
          }
          
          private function messageKeyPressed(event:KeyboardEvent):void {
               newMessage.text = "";
               if(event.keyCode == Keyboard.ENTER) sendMessage();
          }
          

           

           

          If this post answers your question of helps, please mark it as such.

          • 2. Re: Strange TextInput Bug
            jherreso Level 1

            That doesn't work. I mean, with your proposed solution the text is cleared before sending the message.

             

            I tried it anyway (just to see what happens) and as I expected, the message sent is blank and the text input remains with the original text.

             

            jorge

            • 3. Re: Strange TextInput Bug
              ANewFlexUser Level 1

              Just re-order the reset-text command and sendMesasge().

               

              private function sendMessage():void {
                   _comm.sendMessage(newMessage.text);
                   newMessage.text = "";
              }

              private function messageKeyPressed(event:KeyboardEvent):void {
                  

                   if(event.keyCode == Keyboard.ENTER) {
                   sendMessage();
                   newMessage.text = "";
                   }
              }

              • 4. Re: Strange TextInput Bug
                run,ryan! Level 3

                use keyUp instead of keyDown, I think textField didn't finish text changing when keyDown

                Another interesting thing is if you put some extra code like popup an alert window in your sendMessage(), you will get what you want event if you use keyDown.

                • 5. Re: Strange TextInput Bug
                  jherreso Level 1

                  Great! The keyUp event does the trick.

                   

                  Thanks