RSS link icon

Flash components the ComboBox 

 

In this next tutorial series we will learn to make use of all the flash components such as simple buttons and combo boxes, checkboxes and data grid, so this is going to be a quite large tutorial, and I will break it up into sections, as we go through all the components.

In this lesson we will learn to use buttons component to raise click events, and I will introduce the combo box, or the drooped as some calls it, and all is of cause done with flash actionscript 3.0.

And here is what we will be making.

First we will make the combo box switch the box color from blue to green when we select a combo box item. Meaning that we will raise an event on the combo box event change.

Next we will learn to add a new item to the combo box, so you can write something into the textbox and with the button you can raise an event to add an item to the combo box.

Before we to down and dirty with the actionscript coding, we need to do some preparations, like setting up some movieclip graphics, buttons etc.

 

So as you can see by the final result at the top, locate the component panel in flash, (you can find it by going to the top menu -> view -> components) and drag out a button, a textbox and a combobox.

 

actionscript 3.0 how to use combobox component

Select each component and go to the properties panel and give them instance names, I named the button "my_btn", the textbox "my_input" and the combobox "my_ComboBox".

actionscript 3.0 how to use combobox component

Now we will add two items to the combo box, two data elements containing a label name, blue and green.

So select the combo box and locate the parameters panel (by default its docked next to the properties panel).

Click on the dataprovider and add in the information as I did in the image below.

actionscript 3.0 how to use combobox component

 

The last thing we need is the colored box, with the rectangle tool draw a square on the stage with the blue color.

Select it and convert the graphic into a movieclip, give the movieclip an instance name, I named mine "box_mc".

In that movie clip create a new keyframe at frame 2 and change the box color to green.

actionscript 3.0 how to use combobox component

Stay inside the colored box movieclip, now we are ready to do our first actionscripting, even though its just one short line.

Select frame one (the one with a blue box).

In the actionscript panel write:

stop(); 

Now go back to the main stage and select the first (and only) frame on the stage, then bring up the actionscript panel, now we will do some coding, I have broken the code up into small pieces for better understanding, and wrote some comments inside the code so you can follow along.

//This line is just to style the textbox so it has a border.
my_input.border = true;
//Here we add an eventlistener to the combobox, to call a function if the combo box item changes.
my_ComboBox.addEventListener(Event.CHANGE, changeColor);
function changeColor(event:Event):void {
    //just a couple of traces to show you a couple of information on the current combo box items.
    trace(my_ComboBox.selectedIndex);
    trace(my_ComboBox.selectedItem.label);
    //an if statement telling flash if the combo box label blue is selected then the box movieclip should change the frame to 1.
    if (my_ComboBox.selectedItem.label == "Blue") {
        trace("is blue");
        box_mc.gotoAndStop(1)
        //an if statement telling flash if the combobox label green is selected then the box movieclip should change the frame to 2
    } else if (my_ComboBox.selectedItem.label == "Green") {
        trace("is green");
        box_mc.gotoAndStop(2)
    }
}

Now that we how we worked with the combo box, how to call a function when we select another item from the dropdown list, and used it to make some changes on the stage.

Now just for fun I added a small function where you can add your own items to the combo box.

// first and eventlistener to listen to when someone click the button, then it calls a function.
my_btn.addEventListener(MouseEvent.CLICK, addToCombo);

// this is the function, the function basically adds an item to the combo box, containing the my_input text from the textbox.
function addToCombo(event:Event):void {
my_ComboBox.addItem({label:my_input.text});
}

Admin Bob says: Sunday, November 30, 2008

hi Asif, hmm I not sure what is wrong with your code, but send me an email with the code and I will try to fix it for you.

support@0tutor.com


Asif says: Friday, November 28, 2008

Hi Dear!

Actaully I tried to use your code but, i dont know why i am getting error:"The class or interface 'Event' could not be loaded".
- function changeColor(event:Event):void {

So please can you assist.
Rest of your other tutorials are very much valuable.
Keep up the good work.

Thanks!
May God Bless You


Jesse says: Thursday, August 28, 2008

Thank you for this... There are lots of tutorials for changing advanced parameters, and so few that clearly cover the basics. This helped a lot!


Ka-Z says: Saturday, August 16, 2008

Nice tutorial...Thnx Man


Hemn says: Sunday, August 10, 2008

IChr(34)m Hemn from Iraq-Kurdistan, I want to thank you for this article it clearly understood me how to use these two properties of combobox. Thank you alot, may Allah reward you ...


Marcus says: Tuesday, July 22, 2008

Wow!
Great Tutorial!
Rating:
Wow! Haha, thanks man.


   


 

 

 

 5

 
 
   Web Premium
 
 

All rights reserved, Copyright 2008. Contact