PDA

View Full Version : [AS3] comboBox problem


matt_sh
06-17-2009, 10:17 AM
Hi,

I'm pretty new to AS3 so you'll have to excuse my lack of knowledge!

I'm building a media card calculator which has three levels of comboBox.

First the user chooses a size of card eg 2GB, 4GB, 8GB, 16GB
Next they choose the device eg camera, camcorder etc
Finally they choose the format of device eg 5, 10, 14 megapixels if it's a camera, HD or non-HD if it's a camcorder.

I'm having probs trying to get the final ComboBox to change it's contents according to what was selected above (so if camera selected the change the box to , 10, 14 megapixels) . My codes is as follows:


deviceDrop.addEventListener(Event.CHANGE, deviceHandler);

deviceDrop.addItem({label:"Choose a device"});
deviceDrop.addItem({label:"Camcorder"});
deviceDrop.addItem({label:"Camera"});
deviceDrop.addItem({label:"MP3"});

function deviceHandler(event:Event):void {

if (deviceDrop.text == "Camcorder"){
formatDrop.addItem({label:"HD"});
formatDrop.addItem({label:"Normal"});
}
else if(deviceDrop.text == "Camera"){
formatDrop.addItem({label:"5 Megapixels"});
formatDrop.addItem({label:"10 Megapixels"});
formatDrop.addItem({label:"12 Megapixels"});
formatDrop.addItem({label:"14 Megapixels"});
}
else if(deviceDrop.text == "MP3"){
formatDrop.addItem({label:"MP3"});
formatDrop.addItem({label:"AAC"});
formatDrop.addItem({label:"WMA"});
}
}
The result is that on the first click nothing happens, second click it had HD and Normal, third click add Megapixels and forth click add MP3 etc.

Hope this is enough info.

Any suggestions would be greatly appreciated

cheers,

matt

palmjack
06-17-2009, 10:49 AM
First of all this code is wrong:
deviceDrop.text == "Camcorder"
change it to:
deviceDrop.selectedLabel == "Camcorder"

matt_sh
06-17-2009, 11:19 AM
Thanks PalmJack,

I've changed the .text to .selectedLabel and it now responds to the first click.

Second problem is that if I click the another item in the comboBox it just add it on to the bottom of the list and each time I click Camera, Camcorder etc it adds more items to the comboBox below. How do I stop it doing that?

thanks!

trendykarn
06-17-2009, 12:03 PM
I think this could help you.

There are two combo box, first one have data as male & female.
so if select male, second one will show only male names (from array).
otherwise it'll show female names.

var genders:Array = ["Select one", {data:"male", label:"Male"}, {data:"female", label:"Female"}];
var disabledDP:Array = ["Choose a gender"];
var maleNames:Array = ["jon", "michael", "robert"];
var femaleNames:Array = ["julia", "helen"];

genderBox.dataProvider = genders;
nameBox.dataProvider = disabledDP;
nameBox.enabled = false;

var cbListener:Object = new Object();
cbListener.change = function(e:Object)
{
var item:Object = genderBox.selectedItem;
if (item.data == "male")
{
nameBox.enabled = true;
nameBox.dataProvider = maleNames;
}
else if (item.data == "female")
{
nameBox.enabled = true;
nameBox.dataProvider = femaleNames;
}
else
{
nameBox.enabled = false;
nameBox.dataProvider = disabledDP;
}
};
genderBox.addEventListener("change",cbListener);

matt_sh
06-17-2009, 01:31 PM
Thanks trendykarn. I have actually found a solution but that code will be useful for future reference.

matt

trendykarn
06-17-2009, 02:02 PM
it's ok