PDA

View Full Version : ComboBox function, selection runs function


xacttech
07-31-2007, 05:30 PM
Hey guys I've got another comboBox one for you...

I'm having a comboBox selection run a function of the same name.

ie:

comboBox.addItem({data:"NAME", label:"Item number 1"});
comboBox.addItem({data:"NAME_2", label:"Item number 2"});

So two items on a comboBox... I need to execute a function after a section is made. here's what I've been doing


var modelListener:Object = new Object();
this.comboBox.addEventListener("change", comboChange);
comboChange.change = function(model_evtObj:Object) {
if (comboBox.selectedItem.data == "NAME") {
NAME();
}
if (comboBox.selectedItem.data == "NAME_2") {
NAME_2();
}


If I could create a listener that automatically executed a function of the same string held in the comboBox data, I'd be set.

Here's my Dummy Logic:
if combo box selected data = something, run something();

No matter what the data is, run a function of the same name.

Thanks Guys!!

xacttech
08-01-2007, 01:11 AM
Out of curiosity how come this was moved?

CyanBlue
08-01-2007, 06:17 PM
I moved your thread because I thought that the Component forum best fits your question... :)

tg
08-01-2007, 06:59 PM
try:

//as 2 code
import mx.utils.Delegate;

comboBox.addEventListener("change",Delegate.create(this,comboChange));
comboChange=function(){
this[comboBox.selectedItem.data]();
}


so. should work. basically the [] will evaluate teh data value of the combobox selection.