AS3 ComboBox extremely basic example


/ Published in: ActionScript 3
Save to your folder(s)

This is the most simple example of a ComboBox created in AS3, based mostly on Adobe's example from http://help.adobe.com/en_US/ActionScript/3.0_UsingComponentsAS3/WS5b3ccc516d4fbf351e63e3d118a9c65b32-7fa9.html

Inside the zip file you will find two folders. The first is this example with a ComboBox dragged onto the stage of the FLA with a supporting .AS file to populate it and make it work. The second example shows a ComboBox created and populated entirely by actionscript.


Copy this code and paste it in your HTML
  1. package {
  2. import flash.display.MovieClip;
  3. import flash.events.Event;
  4.  
  5. import fl.events.ComponentEvent;
  6. import fl.controls.ComboBox;
  7. import fl.data.DataProvider;
  8.  
  9.  
  10.  
  11.  
  12. public class AS3_combobox2 extends MovieClip {
  13. var comboData:Array = new Array(
  14. {label:"Choice One", data:"one"},
  15. {label:"Choice Two", data:"two"}
  16. );
  17.  
  18. var combobox_cb:ComboBox = new ComboBox();
  19.  
  20. public function AS3_combobox2() {
  21. combobox_cb.dropdownWidth = 210;
  22. combobox_cb.width = 200;
  23. combobox_cb.move(150, 50);
  24. combobox_cb.prompt = "Make a Choice";
  25. combobox_cb.dataProvider = new DataProvider(comboData);
  26. combobox_cb.addEventListener(Event.CHANGE, changeHandler);
  27.  
  28. addChild(combobox_cb);
  29. }
  30. public function changeHandler(event:Event):void {
  31. // do something based on the selected item's value
  32. switch(combobox_cb.selectedItem.data) {
  33. case "one":
  34. trace("One was chosen!");
  35. break;
  36. case "two":
  37. trace("Two was chosen!");
  38. break;
  39. }
  40. }
  41.  
  42.  
  43.  
  44.  
  45. }
  46. }

URL: http://dl.dropbox.com/u/316550/code-AS3_combobox.zip

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.