Skip to main content
Inspiring
March 21, 2011
Question

ComboBox question2

  • March 21, 2011
  • 1 reply
  • 417 views

Help

Question on Combo Box.   Is there an option for the user to type in a search word and it will go to that item?  In Visual studio I have an option for this. Currently the way it works it only jumps to the first letter of the word, kind of a intellegent search......  Hopefully this makes sense....

Thanks again to dmennenoh for assisting me on my last question, this forum is great..

Thanks. mesa.....

This topic has been closed for replies.

1 reply

March 22, 2011

There's no option, but you can build something. You can add a  keyboard listener to the combo box, and then add typed letters to a  search string. Then search the data provider's label property for you  search string.... Something like so should give you a start:

combo.addEventListener(Event.OPEN, comboOpened, false, 0, true);
combo.addEventListener(Event.CLOSE, comboClosed, false, 0, true);

var searchString:String;

function comboOpened(e:Event):void

     combo.addEventListener(KeyboardEvent.KEY_DOWN, searchKeyPressed, false, 0, true);

    searchString = "";

}

function comboClosed(e:Event):void

{

     combo.removeEventListener(KeyboardEvent.KEY_DOWN, searchKeyPressed);

}

function searchKeyPressed(e:KeyboardEvent):void

{

    searchString += String.fromCharCode(e.charCode); 

    for(var i:int = 0; i < combo.dataProvider.length; i++){

        if(String(combo.dataProvider.getItemAt(i).label).toLowerCase().indexOf(searchString) == 0){

            combo.selectedIndex = i;

        }

    }

}

Inspiring
March 22, 2011

This is awesome, 1st how you deciphered what I was trying to do with the little to no description that I wrote (because after reading it back to myself, I was like wow, what the heck am I saying).  2nd, I can’t thank you enough for your assistance again….. I will start looking at the code you provided, but quickly looking at it, it makes sense. 3rd, I'm hoping that someday soon I can get to your level in AS3..  Again many thanks for your help.....mesa