Digital Survivors
 

Control objects with a ComboBox in Flash MX

Scott Manning
November 11, 2002 | Comments (11)

It took me forever to find a good title for this tutorial and I still don't think it completely describes what I'm going to show you. I want to create a dropdown field that will determine whether or not a textbox can be edited. Also, if a textbox cannot be edited, any data in it is meaningless and needs to be removed. This is a very useful feature for online forms and Rich Media Applications.

Here's a working sample of how it could be used:

The requirements for this should be obvious. If the user does not visit Scottmanning.com daily, then asking them 'how many times a day?' is pointless. So the textbox is made read only. If they do in fact visit the site daily, the textbox can be edited. In the event that the user changes their mind about actually visiting the site daily, the textbox needs to become read only again and any data in it should be removed.

Step-by-step
Making this work is simpler than you'd think, but you do need Flash MX. Here's a step-by-step process on how it's done:

1. Open Flash MX and start a new Flash movie.

2. Open the components panel (Window > Components) and select the 'Flash UI Components'.

3. Select the ComboBox component and drag and drop it into the scene of the Flash movie.

dropdown.selectcombobox (4k image)


4. With the ComboBox in your scene selected, open the Parameters panel (Window > Properties and click the 'Parameters' tab).

5. Give it an instance name of 'dropdown'.

6. Add the following values in the 'Labels' field in this order: 'Select', 'Yes', and 'No'. Whatever item is first in this list will be the dropdown's default value.

7. Add the following values in the 'Data' field in this order: 1, 2, and 3. The values you select for this field will line up with the values you inputted for the 'Labels' field. For example, 'Select' will have a data value of 1 and 'Yes' will have a data value of 2.

When all is said and done, your Parameters panel for the ComboBox component should look like this:

dropdown.properties (4k image)


8. Next, you will need to create the text field that is controlled by the ComboBox. Select the Text Tool on the Tools panel (Window > Tools) and draw a text field in the scene under the ComboBox.

9. With the text field selected, go to the Properties panel (Window > Properties). Set the type of text field to 'Input Text', give it an Instance name of 'howmany_properties', give it a variable name of 'howmany', and select to 'Show Border Around Text'.

Ultimately, your Panel for the textbox will look like this:

dropdown.textfield.properties (7k image)


10. Now you will need to add some code to the Flash movie that will continually check the status of the dropdown field. Based upon the results of the check, it will either make the textbox read only or make it so it can be edited.

Add this following code to a frame in the same spot in the Timeline as your textbox and dropdown:

stop();
// Textfield is only relevant if you visit the site daily
function dropdown_check() {
        if (dropdown.getValue() == 2) {
                // If "Yes", then the howmany can be edited.
                howmany_properties.background = false;
                howmany_properties.type = "input";
                howmany_properties.selectable = true;
        } else {
              // If "No" or Select", then the howmany cannot be edited.
                howmany_properties.background = true;
                howmany_properties.backgroundColor = 0xcccccc;
                howmany_properties.type = "static";
                howmany_properties.selectable = false;
                // Set the value to zero in case the user changed their minds.
                howmany = 0;
        }
}
// Check the status of the dropdown every 10 miliseconds.
setInterval(dropdown_check, 10);
// ------------
// Embed Arial and Veranda fonts
globalStyleFormat.embedFonts = true;
globalStyleFormat.textFont = "MyArial";
globalStyleFormat.textFont = "MyVeranda";
globalStyleFormat.applyChanges();
_root.values._visible = 0;


The code broken down
For those of you wish to gain an understanding what is going on, here is the code broken down with explanations.

stop();

This is merely telling the Flash movie to stop playing.


function dropdown_check()

This is the function that will be checking the status of the dropdown and determining what the state of the textbox should be.

{
        if (dropdown.getValue() == 2) {
                // If "Yes", then the howmany can be edited.
                howmany_properties.background = false;
                howmany_properties.type = "input";
                howmany_properties.selectable = true;

This says that if the dropdown field has a value of '2' or 'Yes', then there will be no background to the textbox, the textbox should is now an input field, and the text in it can be selected.

        } else {
              // If "No" or Select", then the howmany cannot be edited.
                howmany_properties.background = true;
                howmany_properties.backgroundColor = 0xcccccc;
                howmany_properties.type = "static";
                howmany_properties.selectable = false;

If the dropdown field is not set to 'Yes', then the textbox will have a grey background, the user cannot input text into it, and the text inside will not be selectable.

                // Set the value to zero in case the user changed their minds.
                howmany = 0;
        }
}

Also, if the dropdown field is not set to 'Yes', the textbox will have a value of zero, thus removing any number the user may have inputted into it.

// Check the status of the dropdown every 10 miliseconds.
setInterval(dropdown_check, 10);

This says to run the previously defined dropdown_check function every ten milliseconds.

// ------------
// Embed Arial and Veranda fonts
globalStyleFormat.embedFonts = true;
globalStyleFormat.textFont = "MyArial";
globalStyleFormat.textFont = "MyVeranda";
globalStyleFormat.applyChanges();
_root.values._visible = 0;

This code is needed to prevent any drunken components. To learn more about this read How to fix drunken components nested in a component.

Sample file
To help most of us out who just prefer to pick apart an FLA file, here you go:

dropdown.icon (1k image)

Enjoy this article? There's more. Digital Survivors is a source of articles, tutorials, reviews, and commentary on all things digital.

Stay informed with rss, our feed is permanently ad-free.

These articles are new:

bullet Valkyrie
bullet 1940 Aircraft Production Figures
bullet Let Teachers Carry Concealed Firearms at Schools
bullet 50 Books on World War II Recommended by John Keegan

If you don't immediately see what you want, you can start a topic in our forums.

 



Comments (11):
1) Posted by: mehdi
November 18, 2003 2:05 AM

cool guide ;)
please help me in other way:
I want to have a function that, jump to special frame number in time line.
item1 --> gotoandplay(1)
item2 --> gotoandplay(2)
... ...

thank you.


2) Posted by: Catacyzm
February 10, 2004 7:04 AM

I enjoyed this information alot. I was wondering if you might be able to help me with another problem I am having. I am trying to set up ComboBoxes up so they are dependat of each other. as it depends on what selection is made in one determines what selections are available in another. I have found a couple tutorials on doing this, but they are all for flash 6 and I want to use flash 7 ... and it doesn't seem to work in flash 7 do to the fact you cannot set the changehandler. I tried doing this dynamically in code with:
name_of_ComboBox.setChangeHandler(name_of_handler);

but does not work.

Please help


3) Posted by: Uzi Shamas
March 29, 2004 12:17 AM

Very good explanation ,and information ,thanks.
I built a simple combobox whith 3 url options, and I want to have a function that link every name in the combo to the specified url site, can you help me, pls.


4) Posted by: Anthony
April 22, 2004 10:07 AM

Thanks, your componant saved our bacon when we were forced to publish a movie made with MX2004 as FP6... damn non-backwards-compatible componants!


5) Posted by: Jason
April 22, 2004 1:53 PM

Thanks for the info...


6) Posted by: Jason
April 22, 2004 1:55 PM

Thanks for the info...


7) Posted by: Mary
June 3, 2004 9:57 AM

Hi Scott,
This page is very informative and useful. Thank you for posting it. One question I need to ask is, when I try to download the .fla file, I receive an html page with a lot of messed up code. Could this be because I am using a mac, and this was produced with a pc, or do you know of another reason.

Thank you.


8) Posted by: Swati
July 26, 2004 6:36 AM

setInterval(dropdown_check, 10);

I am not able to understand this why this use in movie


9) Posted by: Collin Siebert
January 13, 2005 9:29 PM

I'm setting up an app. for users, and I was running into a problem with the drop and drag function of Flash MX 2004 pro.

I want the users to be able to drag a box to anywhere on the screen and then be able to type in text into the box

basically be able to drag the text input field.?

I have not be able to find info on anything realite to this.?
does anyone know if this is possible in Flash??


10) Posted by: ram
May 12, 2006 5:56 AM

i have openend the combo box, now it is not closed .
i want to automatically close or restore to the original position, how to do


11) Posted by: ravi
August 11, 2006 3:42 AM

I have a query in flash.
i.e how to display the content of respective array/ dataset/tables in a datagrid after selecting a field in combo box?

Suppose, A combo box contains Name of Nation.
Array/Tables/Dataset contains info about Nation.

So, after selecting a nation from combo the corresponding info should have display in datagrid.
How we'll do it in Flash???


border