Search code examples
flashactionscript-3actionscriptflash-cs4

How to get the previous focus and compare strings


How can I get the previous focus/be able to compare strings?


Solution

  • The FocusEvent.FOCUS_IN event contains also a reference to the interactive object losing the focus (relatedObject property).

    In your code you could change:

    textbox[i].addEventListener(KeyboardEvent.KEY_UP, k);
    textbox[i].tabIndex= i; 
    

    to:

    textbox[i].addEventListener(KeyboardEvent.KEY_UP, k);
    textbox[i].addEventListener(FocusEvent.FOCUS_IN, handleFocusIn);
    textbox[i].tabIndex= i; 
    
    function handleFocusIn(anEvent: FocusEvent): void
    {
      if (anEvent.relatedObject is TextField)
      {
         var previousBox: TextField = anEvent.relatedObject as TextField;
         var currentBox: TextField = anEvent.target as TextField;
         // etc.
      }
    }
    

    Like the previous answer said, try to figure out how looping works. Also you can reference the TextField using []:

    // to reference names_mc.box19_txt using an index
    var index: int = 19;
    var textbox: TextField = names_mc['box' + index + '_txt'];