7 Replies Latest reply on Aug 5, 2009 5:50 PM by Dharmesh Chheda

    Focusout issue with Textbox

    Dharmesh Chheda

      Hi,

       

           Here is my issue in a nut shell This is my sample app

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  width="100%" height="200" layout="absolute" xmlns:temp="com.marsh.marketconnect.flex.components.*" creationComplete="initApp()">

       

          <mx:Script>
              <![CDATA[
             
                  public function initApp():void{
                      first.addEventListener(FocusEvent.FOCUS_OUT,helloFirst);
                      second.addEventListener(FocusEvent.FOCUS_OUT,helloSecond);
                  }
                  public function helloFirst(event:Event):void{
                      trace("Hello First");
                      event.stopImmediatePropagation();
                      event.currentTarget.stage.focus=null;
                      
                  }            
                  public function helloSecond(event:Event):void{
                      trace("Hello Second");
                  }  

       

              ]]>
          </mx:Script>

       

          <mx:TextInput id="first" />
          <mx:TextInput id="second" x="219"/>

       

      </mx:Application>

       

       

      If you see above.. when i press <tab> key which losses the focus from the first textInput, both methods are invoked i.e. the helloFirst and also the helloSecond. Why is that .. how do i prevent the second listener from kicking .. I just clicked Tab on the first textInput .. any tips?

       

      Thanks

      Dharmesh

        • 1. Re: Focusout issue with Textbox
          Gregory Lafrance Level 6

          You need to have a single event listenerand check the currentTarget of the event object and if it is not "first" then don't do anything.

           

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

          • 2. Re: Focusout issue with Textbox
            Dharmesh Chheda Level 1

            Hi Greg,

             

                You meant single event listerner?.. listening to what?. meaning only on the first textInput?

            • 3. Re: Focusout issue with Textbox
              Gregory Lafrance Level 6

              Something like this:

               

              first.addEventListener(FocusEvent.FOCUS_OUT,helloBoth);
              second.addEventListener(FocusEvent.FOCUS_OUT,helloBoth);

              public function helloBoth(event:Event):void{
                  if(event.currentTarget == first){
                      event.currentTarget.stage.focus=null;                
                  }
              }      

              • 4. Re: Focusout issue with Textbox
                Dharmesh Chheda Level 1

                Hi Greg

                 

                   As per your suggestion i did the following

                 

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  width="100%" height="200" layout="absolute" xmlns:temp="com..*" creationComplete="initApp()">

                 

                    <mx:Script>
                        <![CDATA[
                       
                            public function initApp():void{
                                first.addEventListener(FocusEvent.FOCUS_OUT,helloBoth);
                                second.addEventListener(FocusEvent.FOCUS_OUT,helloBoth);
                            }
                            public function helloBoth(event:Event):void{
                                if(event.currentTarget==first){
                                    trace("Focus moved from first textInput");   
                                }else if(event.currentTarget==second){
                                    trace("Focus moved from second textInput");
                                }
                               
                            }          

                 

                        ]]>
                    </mx:Script>

                 

                    <mx:TextInput id="first" focusRect="true"/>
                    <mx:TextInput id="second" x="219"/>

                 

                </mx:Application>

                 

                But event now when i move the focus away from text input first.. I get the following trace

                 

                Focus moved from first textInput
                Focus moved from second textInput

                • 5. Re: Focusout issue with Textbox
                  Flex harUI Adobe Employee

                  Setting focus to null might get the FocusManager to kick in.  Once focus is on something, the FocusManager doesn't want focus to be "nowhere".  What's your real goal?

                   

                  Alex Harui

                  Flex SDK Developer

                  Adobe Systems Inc.

                  Blog: http://blogs.adobe.com/aharui

                  • 6. Re: Focusout issue with Textbox
                    Dharmesh Chheda Level 1

                    Hi Alex

                     

                       Here is my requirement. I have a horizontal slider with 2 thumbs. Each thumb displays a value in a text box.. meaning. as i slide a thumb, the corresponding valu in the textInput also changes to the latest value in the thumb. Also If the user does not want to use the slider, he /she can directly change the value in the textInput which will then update the slider thumb position based on what value the user entered.

                     

                    My problem starts here .. there is a min and max in the slider. assuming that the min is 10 and max in 100, I also need to do the same validation in the corresponding text input boxes. and hence the listeners to my textInput boxes.

                     

                    My problem is when i move focus away from textInput1, it invokes the handler for focus out for textinput1 .. but it also invokes the handler for textinput 2..

                     

                    check the latest code i have pasted and the output .. am i doing something wrong?

                     

                    have attached the sectional snapshot of the app i have

                     

                     

                    • 7. Re: Focusout issue with Textbox
                      Dharmesh Chheda Level 1

                      tried everything possible .. still no luck