How to Get Data From a ComboBox in Adobe ActionScript 3.0

ActionScript 3 ComboBoxes store data in a selectable list form. When you select an item from a ComboBox you can retrieve any of the data that is associated with the ComboBox selection. The two Combox data items that are often retrieved are stored in the ComboBox's label and data property. The label data is the text in the ComboBox selections and the data property is data associated with the selection, such as the price of the selected item. Once your ComboBox is populated, you can retrieve the contents of the label or data property with one line of ActionScript 3.

Instructions

    • 1

      Start the Flash program. Click "Flash File(ActionScript 3.0)" from the splash window to create a new file for an AS3 Flash animation project.

    • 2

      Click "Components" from the "Windows" menu. Click-and-drag the ComboBox icon onto the Flash Stage from the Components library. Click "Properties" from the "Windows" menu. Type "comboBox_A" in the "instance name" text box in the Properties dialog box.

    • 3

      Select "Actions" from the Window menu on the main Flash menu bar to open the ActionScript 3 editor. Position your mouse cursor on the first line of the ActionScript 3 editor and type in the code to import the necessary Flash utilities to use the ComboBox, Sprite, Event and TextField classes:

      import fl.controls.ComboBox;

      import flash.display.Sprite

      import flash.events.Event;

      import flash.text.TextField;

      import flash.text.TextFieldAutoSize;

    • 4

      Type in the code in the next line of the ActionScript 3 editor to create a text field that will display the ComboBox's label property's data and the data property's data when a selection item is chosen from the ComboBox:

      var textField1:TextField;

      textField1 = new TextField();

      textField1.x = 230;

      textField1.y = 15;

      textField1.autoSize = TextFieldAutoSize.LEFT;

      addChild(textField1);

    • 5

      Type in the code starting on the next line of the ActionScript 3 editor to create a ComboBox that stores the selectable payment methods and the associated charge data associated with each payment selection:

      comboBox_A.setSize(200, 22);

      comboBox_A.prompt = "Select a Method of Payment";

      comboBox_A.addItem( { label: "MasterCard", data:1123 } );

      comboBox_A.addItem( { label: "Visa", data:2200 } );

      comboBox_A.addItem( { label: "American Express", data: 1000} );

      comboBox_A.addItem( { label: "Paypal", data: 2000 } );

      comboBox_A.addItem( { label: "Cash", data: "not a penny" } );

      comboBox_A.addItem( { label: "Check", data: 1300} );

    • 6

      Type in the code starting on the next line of the ActionScript 3 editor to add an event listener to the ComboBox that will detect if a selection in the ComboBox list has been made and, if it has, instruct Flash to execute the code in the function named "paymentMethod":

      comboBox_A.addEventListener(Event.CHANGE, paymentMethod);

    • 7

      Type in the code starting on the next line of the ActionScript 3 editor to create the "paymentMethod" function so that the function code will display the data and label data associated with the selected item (from the ComboBox):

      function paymentMethod(e:Event):void {

      textField1.text = "Your "

      textField1.appendText(comboBox_A.selectedItem.label);

      textField1.appendText(" account has been charged $");

      textField1.appendText(comboBox_A.selectedItem.data);

      }

    • 8

      Copy and paste the code into the ActionScript 3 editor to ensure that the ComboBox Get Data program has no syntax errors and runs correctly:

      import fl.controls.ComboBox;

      import flash.display.Sprite

      import flash.events.Event;

      import flash.text.TextField;

      import flash.text.TextFieldAutoSize;

      var textField1:TextField;

      textField1 = new TextField();

      textField1.x = 230;

      textField1.y = 15;

      textField1.autoSize = TextFieldAutoSize.LEFT;

      addChild(textField1);

      comboBox_A.setSize(200, 22);

      comboBox_A.prompt = "Select a Method of Payment";

      comboBox_A.addItem( { label: "MasterCard", data:1 } );

      comboBox_A.addItem( { label: "Visa", data:2 } );

      comboBox_A.addItem( { label: "American Express", data: 1000} );

      comboBox_A.addItem( { label: "Paypal", data: 2000 } );

      comboBox_A.addItem( { label: "Cash", data: "not a penny" } );

      comboBox_A.addItem( { label: "Check", data: 1300} );

      comboBox_A.addEventListener(Event.CHANGE, paymentMethod);

      function paymentMethod(e:Event):void {

      textField1.text = "Your "

      textField1.appendText(comboBox_A.selectedItem.label);

      textField1.appendText(" account has been charged $");

      textField1.appendText(comboBox_A.selectedItem.data);

      }

    • 9

      Click "Test Movie" from the Control menu to play the ComboBox Get Data program. Click the "Select a Method of Payment" text label and then select one of the payment options. Observe that the text entered into the text field and the contents of the label and data property are now displayed in the text field on the stage.

EduJourney © www.0685.com All Rights Reserved