PDA

View Full Version : [AS3] ComboBox issues


ChristopherWright
04-21-2009, 12:06 PM
Hi -
Can anyone help me - I am learning AS3 and am working with a ComboBox.
I want to set up a class in an .as file to use as a data provider and have a ComboBox in an .fla file link to it.

I have been able to programatically generate a CB in a .as file, and when tested it seems to work, in that it shows up, the list drops down, and I can select items in the list...but I want to pass the info from the item selected on to other parts of my program and it seems to not be doing that.

I can also build a CB in an .fla file, populate it with data and that works fine, but I want to link the .fla CB to the data provider in the .as file.

I have read the tutorials and online help, and gone through the printed help files, but don't seem to be able to find this particular solution...

So - short version - how do you link a CB in an .fla file to a dataProvider in an .as file?
Thanks -
Christopher in Buffalo

xty
04-21-2009, 12:48 PM
Hi There
When you programatically generated the CB what were you using as a dataprovider?
And what do you want to pass on when an item is selected in the CB?
Answer that and I'll try to help you out!

ChristopherWright
04-21-2009, 01:30 PM
Hi and thanks for asking -
In one version of the .as file I want to use as a data provider I have set up like this (snipped):
var usrDP:DataProvider = new DataProvider();
usrDP.addItem({label:"Buffalo, NY, USA",data:"42.939514,-78.888113,-5.0"});// Lat 4256'22.25N Lon 7853'17.21"W

var cityComboBox:ComboBox = new ComboBox();
cityComboBox.prompt = "Your Location:";
cityComboBox.dataProvider = usrDP;
cityComboBox.move(10, 40);
cityComboBox.setSize(220,24);
addChild(cityComboBox);


I might also like to try it with a variant that would populate the usrDP with data formatted as such:
var usrDP:DataProvider = new DataProvider();
usrDP.addItem({usrLoc:"Buffalo, NY, USA",usrLat:"42.939514", usrLon:"-78.888113", usrUTC:"-5.0"});
I could either access the label and data sections (comma deliniated) or in the second case, access each variable such as var usrLat and var usrLon.
I need to use these values in calculations based on what the user has chosen.

Either version would be useful to the project I have in mind, but without being able to figure out how to get the class that the data is in talk to the CB that will be placed in the .fla I will not be able to go on writing the rest of the application...or am I thinking in the wrong way?

It seems to me that building what will be an admittedly large populated CB as a class that may or may not be accessed by a user makes more sense than piling all of that info into a nav bar that the user would be forced to load frequently...

Does this illuminate my dilemma more? If there is a sample file somewhere showing what I want to do and I have missed it, I am more than happy to use it a not bug people, but this is driving me crazy...
Christopher

xty
04-21-2009, 01:50 PM
I like your second dataprovider better, but would use the label:"Buffalo, NY, USA" since I assume that is what you want the user to pick from the list.
When you set up the comboBox (cityComboBox) add an eventListener like so:

cityComboBox.addEventListener(Event.CHANGE, myListenerFunction);

myListenerFunction is the name of the function that the code will go to when someone selects an item from the comboBox - you can call it whatever you want.
Now you need to set up that function like so:

private function myListenerFunction(e:Event):void{
trace("Latitude: " + e.target.selectedItem.usrLat);
}

Try that and you should get the latitude for whichever item is selected from the comboBox. To get the other variables attached just substitute usrLat for usrLon or usrUTC.
Let me know how you make out.

ChristopherWright
04-21-2009, 02:09 PM
I like the second version better also - it seems more usefull in the long run...
So - lemme get this straight... the line:
cityComboBox.addEventListener(Event.CHANGE, myListenerFunction); goes in the .as file with the major data and the myListenerFunction goes in the client .fla file?

This is where I am confused, and appreciate your help...Do I need to have an instance of the CB built into the .fla and have it ref the .as or do I leave it out and have the .as programatically insert one onto the stage of the .fla. I have looked at so many examples my head is fuzzy.

So if I format the DP as such:var usrDP:DataProvider = new DataProvider();
usrDP.addItem({label:"Buffalo, NY, USA",usrLat:"42.939514", usrLon:"-78.888113", usrUTC:"-5.0"}); I can see how that would also be better. Quick side question - will I need to cast the usrLat, usrLon and usrUTC variables from string type to number type for numeric calculations with them?

xty
04-21-2009, 02:20 PM
No all of this is taking place in the as file. so your comboBox should be:

var cityComboBox:ComboBox = new ComboBox();
cityComboBox.prompt = "Your Location:";
cityComboBox.dataProvider = usrDP;
cityComboBox.move(10, 40);
cityComboBox.setSize(220,24);
cityComboBox.addEventListener(Event.CHANGE, myListenerFunction);
addChild(cityComboBox);

The function (myListenerFunction) should be in the same as file.

I know it gets confusing with stuff spread out all over AS files and fla's. That is why I simply use Classes for everything. Unless I have a graphic to use as a framework, I have nothing in my fla file except the Document class (under properties) set to the name of the as file.

ChristopherWright
04-21-2009, 02:54 PM
Thanks - I will try it and see if I can get it to work...If I can build a small model then I will have to go in and rebuild the first DP into the new 2nd DP format...

xty
04-21-2009, 03:37 PM
If you're still having trouble feel free to email me - [email protected]