tags:

views:

518

answers:

1

Hello,

I'm attempting to search a combobox based on text entered via a keyboard event. The search is working and the correct result is being selected but I can't seem to get the scrollToIndex to find the correct item which should be the found result (i). It's scrolling to the last letter entered which I believe is the default behavior of a combobox. I think I'm referring to the event target incorrectly. Newbie tearing my hair out. Can you help? Thank you. Here's the function:

private function textin(event:KeyboardEvent):void 
{

var combo:ComboBox = event.target as ComboBox;

var source:XMLListCollection = combo.dataProvider as XMLListCollection;

str += String.fromCharCode(event.charCode);

if (str=="") {
  combo.selectedIndex = 0;
}

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

    if ( source[i][email protected](new RegExp("^" + str, "i")) ) {
     combo.selectedIndex = i;
     event.target.scrollToIndex(i);
     break;
    }

}
}

Control:

<mx:ComboBox keyDown="textin(event);" id="thislist" change="processForm();" dataProvider="{xmllist}"/>
A: 

If event.target is a mx.control.ComboBox then it doesn't have a scrollToIndex method, which is a method defined in mx.controls.ListBase, which the ComboBox doesn't inherit from. Check the api reference for the ComboBox. What exactly is the result you a you are trying to achieve here? If you set the selected index of a ComboBox it should display the item at that index.

EDIT: Try getting replacing event.target.scrollToIndex(i) (which should throw an error anyway) and replace it with event.stopImmediatePropagation(). This should prevent whatever the default key handler is from firing and overriding your event handler.

Ryan Lynch
Thanks, Ryan. The selectedIndex displays the item at that index successfully. I thought I read somewhere that ComboBox did inherit from ListBase. I'm trying to show the reasonable list below the selectedIndex. So if Anagram is the selected item in a list of (Airline,Anagaram,Ant,Apple,Orange,Pear,Nukes) then Ant would be the next item in the scroll when you enter "n" not Nukes.
Kerri
I think I understand what you are trying to achieve here. What control is the user entering the text into? Post some code showing the controls and how you attached the event handlers to them. If you could fix the formatting on the code you already posted that would be nice too. Just add some spaces before the lines that aren't getting formatted correctly.
Ryan Lynch
Tried to clean it up a bit and at the end I added what the control looks like. I'm basically trying to allow the user to search and browse a long list via a combobox. Once selected it processes the form. Once the combobox is closed and re-opened, the list is in the correct order under the selected item as expected.
Kerri
Try making the change in the last edit. I'm assuming `str` defined outside of the function so that its value carries over between calls. Do you have some event that resets the string to a blank string at some point? Like a timer event or something?
Ryan Lynch
Yeah - str is set outside the function and a timer is called as well. I didn't include because it didn't seem relevant to the problem. I'm wondering why I'm not getting any errors either. event.stopImmediatePropagation() didn't help it. I think I'll look into another way. I'm apparently trying to build in functionality to ComboBox that isn't there - albeit I think it should be there. Will take another look at Adobe's autocomplete and will post here what I learn for other folks. Thanks again, Ryan.
Kerri
You said you are a noob so don't flip out at this question...but you do have the debug version of Flash player installed right?
Ryan Lynch
Hi Ryan - I do but just recently started to appreciate it. What I want to accomplish with the combobox doesn't feasible for my timeline right now. After a bunch of scouring online it seems there are options close to what I want to do out there. Will come back around to it.
Kerri