ok, with 2 combo Boxes on the stage (called funs_cb, a_cb). Assign the document class the name of AutoComplete.
package
{
import flash.events.MouseEvent;
import fl.events.ComponentEvent;
import flash.events.Event;
import fl.controls.TextInput;
import fl.controls.ComboBox;
import flash.display.MovieClip;
import fl.data.DataProvider;
public class AutoComplete extends MovieClip
{
var currentIndex:Number
var initDP:DataProvider;
var initArray:Array;
var FUNSList:Array = ["A + B", "A - B", "(A + B)/2", "A/2 + B", "A/4 + B/2", "(A + 2*B)/3", "A * B", "-A * B", "A * 10^B", "|A + B|", "|A - B|", "Min(A, B)", "Max(A, B)", "Quantize B to A", "Lowpass(F = A, B)", "Hipass(F = A, B)", "B / (1 - A)", "(A + B)^2", "Sin(A + B)", "Cos(A + B)", "Tri(A + B)", "Warp1(A, B)", "Warp2(A, B)", "Warp3(A, B)", "Warp4(A, B)", "Warp8(A, B)", "A AND B", "A OR B", "B > A", "Ramp(F = A + B)", "Ramp(F = A - B)", "Ramp(F = (A + B)/2)", "Ramp(F = A * B)", "Ramp(F = -A * B)", "Ramp(F = A * 10^B)", "Ramp(F = (A + B)/4)", "A * (B - Y)", "A * (Y + B)", "(A * Y) + B", "(A + 1) * Y + B", "Y + A * (Y + B)", "A * |Y| + B", "Sample B on A", "Sample B on ~A", "Track B while A", "Track B while ~A", "Diode(A - B)", "Diode(A - B + .5)", "Diode(A - B - .5)", "Diode(A - B + .25)", "Diode(A - B - .25)"];
var SRCList:Array = ["A CLOCK","AMPENV","ENV2","ENV3","ASR2","ASR1","FUN4","FUN3","FUN2","FUN1","LFO2","LFO1", "2", "0.2", "-.1", "3", "3.5", "3.67"]
public function AutoComplete()
{
currentIndex = 0;
initDP = new DataProvider();
initArray = new Array();
addListeners();
fill_init_dp(FUNSList, "funs_cb");
fill_init_dp(SRCList, "a_cb");
}
public function addListeners()
{
funs_cb.addEventListener(MouseEvent.MOUSE_DOWN, getCurrentIndex) //when you click
funs_cb.textField.addEventListener(Event.CHANGE, getEntry); //when you type
funs_cb.addEventListener(Event.CHANGE, getChoice) //when you make your choice
funs_cb.editable = true;
a_cb.addEventListener(MouseEvent.MOUSE_DOWN, getCurrentIndex)
a_cb.textField.addEventListener(Event.CHANGE, getEntry);
a_cb.addEventListener(Event.CHANGE, getChoice)
a_cb.editable = true;
}
public function getCurrentIndex(m:MouseEvent)
{
trace (" *****************GetCurrentIndex")
//records selectedIndex when user clicks, and also records current dataProvider of selected ComboBox;
var obj = m.currentTarget;
var i:Number;
var s:String;
var n:String;
currentIndex = obj.selectedIndex;
initDP.removeAll();
initArray = [];
for (i = 0; i < obj.length; i++)
{
s = removeSpaces(obj.getItemAt(i).label);
n = obj.getItemAt(i).label;
initArray.push({norm:n, trunc:s});
initDP.addItem(obj.getItemAt(i));
}
}
public function getEntry(e:Event)
{
//reads user's input, and finds match in initArray.
//shows a dataProvider with only things that match
trace ("\r *********************** getEntry()\r")
var i:Number;
var t:String = TextInput(e.currentTarget).text;
var cb:ComboBox = ComboBox(e.currentTarget.parent);
var temp:Array = [];
var tempDP:DataProvider = new DataProvider();
t = removeSpaces(t).toUpperCase();
for (i = 0; i < initArray.length; i++)
{
var q:String = initArray*.trunc.substr(0,t.length).toUpperCase(); //convert every initArray element into an upperCase string, that is equal in length to the user's input
if (t == q)
{
trace ("Found a Match:", initArray*.norm);
temp.push(initArray*.norm);
}
}
//create temporary DataProvider for CB
for (i = 0; i < temp.length; i++)
{
tempDP.addItem( { data:temp*, label:temp* } );
}
//show the temporary DataProvider to the user with the filtered choices
cb.dataProvider = tempDP;
cb.rowCount = 17;
cb.invalidate();
cb.open();
}
public function getChoice(e:Event)
{
//finds user's choice in initArray(created when you click on the CB)
//reloads the original dataProvider into the CB, and selects the index where user's choice was found.
trace ("\r ********************** getMouseChoice\r")
if (e.target.selectedIndex != -1)
{
var cb = ComboBox(e.target);
var i:Number;
var j:Number;
var choice:String;
choice = removeSpaces(cb.selectedItem.label).toUpperCase();
for (i = 0; i < initArray.length; i++)
{
var n:String = removeSpaces(initArray*.norm).toUpperCase();
if (choice == n)
{
cb.removeAll();
for (j = 0; j < initDP.length; j++)
{
cb.addItem(initDP.getItemAt(j));
}
cb.selectedIndex = i;
return;
}
}
}
}
public function fill_init_dp (ar:Array, targ:String)
{
var i:Number;
for (i = 0; i < ar.length; i++)
{
this[targ].addItem( { data:ar* , label:ar* } );
}
this[targ].selectedIndex = 0;
this[targ].rowCount = 17;
}
public function removeSpaces(n:String):String
{
var i:Number;
for (i = 0; i < n.length; i++)
{
if (n.charAt(i) == " ")
{
n = n.substr(0,i) + n.substr(i+1,n.length)
i = i - 1;
}
}
return n;
}
}
}