11 Replies Latest reply on Sep 6, 2006 5:26 PM by m_ahlenius

    Selection.setFocus problem

    m_ahlenius
      Hi,

      I am a novice at best at Action Script. Am trying to use Selection.setFocus() to highlight a region of text in an input text field. But when I position the cursor using; Selection.setSelection( strLen - 2, strLen - 1 ); it puts a "1" digit inside the text string in the display. Am sure I must be doing something wrong, but can't figure it out.

      strLen is set to the current length of the input text string.

      If I change the position to just (strlen, strlen), it appends a "1" to the end of the input field line.

      Can someone please help me out here?

      thanks much,

      'mark
        • 1. Re: Selection.setFocus problem
          kglad Adobe Community Professional & MVP
          the code you showed wouldn't cause that. show the rest of your code affecting your textfield.
          • 2. Selection.setFocus problem
            m_ahlenius Level 1
            Anyone? Need some help with this weird side effect.

            With Selection.setSelection( x, y), the text underneath and does not get highlighted, but is wiped out and replaced with a "1". Really weird. If I put the selected area at the end of the string, it appends a "1" there.

            Now to be fair, I inherited this code, so some I have only modified this print function with the Selection features... (typed in, not cut and pasted)

            function myPrint() {
            // if this is the first item in the list, don't add a newline char
            newLine = (strLen != 0 ) ? "\n" : "";
            // append the new task to the existing ones
            _root.mcDictation.displayText.text += (newLine + my_str);
            var strLen:Number = _root.mcDictation.displayText.length;
            _root.mcDictation.displayText.backgroundColor = 0xFFFF00;

            Selection.setFocus("_root.mcDictation.displayText");
            Selection.setSelection( strLen - 5, strLen +1 );

            _root.mcDictation.displayText.backgroundColor = 0xFFFFFF;
            }


            ***
            If the input text field contained "Pick up milk on way home", after this function it contains "Pick up milk on way 1"

            I used the trace function on the "my_str" string, and printed it out before and after, and there is no "1" in that string.

            any other clues?

            THANKS!
            • 3. Re: Selection.setFocus problem
              m_ahlenius Level 1
              Hi,

              I did find one more thing - if I change my text field to a "dynamic text" box, the selection function works normally. But as a "input text" field, it does not.

              I have another example from an Adobe Macromedia book which makes a word processor using an input text field, it works fine with Selection.setSelection.

              argh!

              'mark
              • 4. Re: Selection.setFocus problem
                kglad Adobe Community Professional & MVP
                where is my_str defined?

                and strLen is always going to be undefined in myPrint()'s first line.
                • 5. Re: Selection.setFocus problem
                  m_ahlenius Level 1
                  Hi,

                  my_str is a global string defined elsewhere.

                  The problem you observed was becuase I was copying (retyping) the code from another comptuer not on the net and I had a few lines out of order (sorry). I simplified the code and the same problem exists in the code below:

                  function myPrint() {

                  // append the new task to the existing ones
                  _root.mcDictation.displayText.text += (my_str + "\n");

                  var strLen:Number = _root.mcDictation.displayText.length;
                  trace("strLen = " +strLen );

                  _root.mcDictation.displayText.backgroundColor = 0xFFFF00;

                  Selection.setFocus("_root.mcDictation.displayText");
                  Selection.setSelection( strLen - 4, strLen );

                  _root.mcDictation.displayText.backgroundColor = 0xFFFFFF;

                  //myHighlight( strLen - 6, strLen );
                  }

                  ***
                  Now here is the kick, if I change the text field (mcDictation.displayText) to a dynamic field type, the highlighting works ok. But I need an editable text field (input text) and when its set to that mode, the highlighting does not work for me.

                  any clues?

                  thanks for your help.

                  'mark
                  • 6. Re: Selection.setFocus problem
                    kglad Adobe Community Professional & MVP
                    now, strLen is always undefined.
                    • 7. Re: Selection.setFocus problem
                      m_ahlenius Level 1
                      Can you please tell me why its undefined?
                      • 8. Re: Selection.setFocus problem
                        kglad Adobe Community Professional & MVP
                        you're trying to access the length property of your textfield. i suspect you want to check the length of your textfield's text property.
                        • 9. Re: Selection.setFocus problem
                          m_ahlenius Level 1
                          Hi,
                          according to the online doc, as I read it, the two functions are one in the same:

                          ------
                          "length (TextField.length property)

                          public length : Number [read-only]

                          Indicates the number of characters in a text field. This property returns the same value as text.length, but is faster. A character such as tab (\t) counts as one character.

                          Availability: ActionScript 1.0; Flash Player 6

                          Example

                          The following example outputs the number of characters in the date_txt text field, which displays the current date.


                          var today:Date = new Date();
                          this.createTextField("date_txt", this.getNextHighestDepth(), 10, 10, 100, 22);
                          date_txt.autoSize = true;
                          date_txt.text = today.toString();
                          trace(date_txt.length);
                          The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method."

                          ---

                          Am I misunderstanding something?

                          'mark
                          • 10. Re: Selection.setFocus problem
                            kglad Adobe Community Professional & MVP
                            no, you have that right. i have it wrong. (i never knew textfield's had a length property.)

                            so, the part of your code you showed is not causing the problem: the last 3 characters of my_str (if it contains, at least, 3 characters) will be highlighted after myPrint() is called and before anything else gets focus.

                            post a link to your fla and specify the textfield's location, if it's not on stage in the first frame of your _root timeline.
                            • 11. Re: Selection.setFocus problem
                              m_ahlenius Level 1
                              Hi,
                              ok I figured out the problem with the help of a friend of mine. weird timing issue.

                              The code is ok, but what the app is doing is waiting for a keypress ("1" key) by the user to move it to the next state. We have some text put into this text input field by the application. When I have the Selection.setSelection() function highlight a region of the text input field, the keypress is being written to this field and the "1" replaces everything which is highlighted (selected).

                              Its like there is a n-key rollover which keeps the keypress key code until there is some field to print it to.

                              Right now am trying to figure out how to catch the keypress output so it does not write a "1" to the field which has the focus.

                              fun .
                              ideas?

                              'mark