Tutorial on radio buttons, combo box, text fields and SharedObjects

SharedObjects are the Flash version of cookies. These are small files stored on the client computer. There are restrictions as to what programs can read the sharedobject, generally the same program that wrote the file and, possibly, files from the same domain. Shared objects are used to store things such as preferences and passwords to make things more convenient for users. However, they, along with cookies in general,also are used to store information for marketing purposes and may not be so innocent. It is possible to specify privacy settings to not store cookies or SharedObjects or to ask each time an attempt is made to store them.

This tutorial describes a simple use of SharedObjects. To have something to store, I display a set of radio buttons, a combo box and a text field. The radio buttons and the combo box are created in ActionScript. It may be necessary to get symbol instances into the Library ahead of time. I did this by clicking on Window/Components and dragging a radio button to the Stage and then erasing it and dragging a combo box to the State and then erasing it. I made two input text field in the usual way, with static text fields next to each one to serve as a label. I gave each an instance name. I clicked on Window/Components/Button and dragged two buttons to the Stage and gave each a name. The Stage looks like this:

Notice that the text field next to Comment is named ptext. Notice also that the Library has several symbols in it arising from the components.

The ActionScript in the Actions panel creates radio buttons and a combo box. Before showing the code, here is what the opening screen looks like:

The user/customer/client can fill out the form and save it by entering in a name next to the Name for saving label and clicking on the Save info button.

If I then change the information to something else and later decide to get this information back, I can click on Fetch info and radio buttons, combo box and text field will be restored with the information saved.

The code creates the combo box, putting several items in it and then creates the 4 radio buttons, giving each one a label and a value, making each of them point to the same radio button group, positioning each on the Stage using move, and using addChild to make them visible.

The code is:

import fl.controls.*;

var pCombo:ComboBox = new ComboBox();

pCombo.addItem({label:"brownie"});

pCombo.addItem({label:"blondie"});

pCombo.addItem({label:"muffin"});

pCombo.addItem({label:"pound cake"});

pCombo.addItem({label:"berries"});

pCombo.move(230, 200);

addChild(pCombo);

var rbgroup:RadioButtonGroup = new RadioButtonGroup("RBG");

var radio1:RadioButton = new RadioButton();

radio1.label = "Hot Chocolate";

radio1.value = "hot"

radio1.group = rbgroup;

radio1.move(100, 200);

addChild(radio1);

var radio2:RadioButton = new RadioButton();

radio2.label = "Coffee";

radio2.value = "cof"

radio2.group = rbgroup;

radio2.move(100, 220);

addChild(radio2);

var radio3:RadioButton = new RadioButton();

radio3.label = "Tea";

radio3.value = "tea"

radio3.group = rbgroup;

radio3.move(100, 240);

addChild(radio3);

var radio4:RadioButton = new RadioButton();

radio4.label = "Chai";

radio4.value = "cha";

radio4.group = rbgroup;

radio4.move(100, 260);

addChild(radio4);

It is possible to associate a value with the items in the combo box but that was not necessary for this application.

A SharedObject is an object with a data property to which you can affix any number of other properties with names of your choosing. In this implementation, I used properties combo, radio and ptext. The savedata function returns a SharedObject of the specified name if one exists and creates a new one if one doesn't exist. Since this function sets the values in any case, there does not need to be any checking if which it was. The code then stores away the selectedIndex for the selected item of the combo box, the value for the radio button group, and the text for the text field. The last statement

so.flush();

writes out the SharedObject file. This will be done when the application ends and so may not be necessary for all applications.

function savedata(ev:Event) {

var so:SharedObject=SharedObject.getLocal(playersfilename.text);

so.data.combo = pCombo.selectedIndex;

so.data.radio = rbgroup.selection.value;

so.data.ptext = ptext.text;

so.flush();

}

The fetchdata function first checks if a name for the SharedObject file was entered into the appropriate text field. If not, it simply returns. Then there is an attempt to recall the SharedObject. This is the line:

var so:SharedObject=SharedObject.getLocal(playersfilename.text);

The next if statement checks if the returned object has any data. If it doesn't, the function ends.

The code then resets the combo box, radio buttons and text field using information from the SharedObject. For the combo box, this is easy: the selectedIndex property is set. Similarly, for the text field, the text property is set to what was stored. For the radio buttons, after trying other things, what worked was to use a switch statement on the value that had been stored. Each case in the switch statement was the value for a radio button. If it was that value, then the radio button selected property is set to true. This will reset any other radio button selected property to false.

function fetchdata(ev:Event) {

if (playersfilename.text=="") {

playersfilename.text="Enter name for data.";

return;

}

var so:SharedObject=SharedObject.getLocal(playersfilename.text);

if (so.size==0) {

playersfilename.text="No data stored with that name.";

return;

}

pCombo.selectedIndex = so.data.combo;

//determine which button's value was stored

switch (so.data.radio) {

case "hot":

radio1.selected = true;

break;

case "cof":

radio2.selected = true;

break;

case "tea":

radio3.selected = true;

break;

case "cha":

radio4.selected = true;

break;

}

ptext.text = so.data.ptext;

}

The last two lines of code set up the event handling for the two buttons.

savebtn.addEventListener(MouseEvent.CLICK,savedata);

fetchbtn.addEventListener(MouseEvent.CLICK,fetchdata);

It is possible to set a time limit on a shared object and, as indicated above, restrict the applications that can access the shared object.