PDA

View Full Version : Assign variable to Checkbox component?



mdavidwalker
December 29th, 2009, 05:15 PM
I am trying to assign a few variables to checkboxes that I am adding dynamically to the stage. There are like 40 checkboxes added dynamically and I need to keep track of a few variables for each checkbox.

It will only let me assign a "name" to the checkbox. If I try something else like "Group" (see code), then I get a runtime error:

Error #1056: Cannot create property Grouping on fl.controls.CheckBox.

Does anyone know how to assign variables to checkboxes?
Is it because its a component?

for (var i:Number = 0;i<array.length;i++)
{
cb = new CheckBox();
var mc_cb = checkboxHolder.addChild(cb);
mc_cb.name = array[i].Id;
mc_cb.Group = array[i].Grouping;

}

mdavidwalker
December 29th, 2009, 05:37 PM
I ended putting each checkbox in its own movieclip and assigning a variable to that movieclip, then I just say e.currentTarget.parent.Group.

creatify
December 29th, 2009, 07:26 PM
You can probably extend the checkbox class and add that as a public variable to the class, or you can just set up a "look up" system based on the name. Creating container movieclips for each seems a bit overkill imho.



var dict:Dictionary = new Dictionary();

for (var i:Number = 0;i<array.length; i++) {
var cb:CheckBox = new CheckBox();
cb.name = array[i].Id;
dict[array[i].Id] = array[i].Grouping;
}
// if you cb is named "cb1" for example:
trace(dict[cb.name]);
trace(dict["cb1"]);

djimmrtl
January 25th, 2010, 02:19 PM
Do you have an example of that? I'm trying to build a similar form that groups check boxes and I'm having some difficulty.



I ended putting each checkbox in its own movieclip and assigning a variable to that movieclip, then I just say e.currentTarget.parent.Group.