0 Replies Latest reply on May 30, 2007 7:02 PM by greg.yachuk

    Can't select text in a ComboBox

    greg.yachuk
      I've got an editable ComboBox and under certain conditions, I want to put specific text into the TextField and set the Selection to some of the characters. I've tried a number of approaches but the caret always goes right before the first character in the TextField of the ComboBox.

      Here are some details. My goal is to reduce the elements in the dropdown menu to only ones that match what the user has typed in (the ComboBox is editable, don't forget). I'm using the filterFunc of a DataSet component to do this. When a enough is typed for a unique match, I want to copy the label into the TextField and select the text from the character that made it unique to the end, so that the user can enter a single backspace to get back to the list they had.

      Here's an example. When I type "A" into the textfield, the dropdown opens and only displays items that begin with the letter "a". As I type in subsequent letters, the set of matching items gets smaller. When I've typed in "Abb", my dropdown has only two items: "Abbeville" and "Abbottville". When I type in the next "o" character, "Abbotville" is the unique match. At that point, I want to copy "Abbotville" into the textfield of the combobox and set the selection to be "otville". If the user hits backspace, the text value is back to "Abb" and both "Abbeville" and "Abbotville" show up in the menu.

      Likewise, I want to select the appropriate characters when the characters in the text field no longer match any entries. E.g. "Abba" would close the menu and highlight the last "a".

      I've put in lots of traces and have confirmed that the Selection object is correct. I used a focusListener to trace the Selection and focusIn and focusOut event listeners and it seems that Selection loses its beginIndex and endIndex almost immediately after the ComboBox change handler returns.

      I don't really want to give the code out because I'm developing it under contract, but here the essential lines of code are this.

      [CODE]
      if (this_cb.length == 1)
      {
      // Only one selectable item. Copy it into the text field and close the box.
      this_cb.close();
      this_cb.selectedIndex = 0;
      this_cb.textField.text = this_cb.dataProvider.getItemAt(0).city;
      // Select any trailing characters.
      Selection.setSelection(this_cb_value.length-1, this_cb.textField.length);
      trace("Selection="+Selection.getFocus()
      +"["+Selection.getBeginIndex()+","+Selection.getEndIndex()+"]:"
      +typeof(Selection.getFocus()));
      }
      [/CODE]

      Right after typing "Abbo", the output window shows the expected data. The selected object is the text object and Selection thinks that characters 3 through 10 should be highlighted.

      "Selection=_level0.quicktype_cb.text_mc.label [3,10]:string"

      And the change handler returns.

      Similar trace statements at the beginning of the focusIn and focusOut handlers show that the begin and end indexes aren't what was set right before.

      "Selection=_level0.quicktype_cb.text_mc.label [-1,-1]:string"

      And sure enough, the caret appears at the beginning of the text rather than highlighting characters 3 through 10. When debugging, I can see that the selection gets set properly for a moment and then the caret is moved to the beginning of the text.

      Thanks for slogging through this post. Any ideas?
      Please let me know if I can clarify anything.