Solved

Dynamic checkbox names

Posted on 2009-03-31
8
602 Views
Last Modified: 2013-11-11
Hello,

I have around 70 checkboxes in my AIR application, which all have names like: c1 - c70 (this means that first checkbox has id="c1" , second id="c2"..... id="c70")

Now i would like to check all 70 checkboxies and determine which one is selected and which one is not.  I would like to use one for loop because it would be too much to write 70 if conditions to check all that manualy.
0
Comment
Question by:Didica
  • 5
  • 3
8 Comments
 
LVL 12

Expert Comment

by:scooby_56
ID: 24032025
create an array to hold the controls

var checkBoxes:Array = new Array();
checkBoxes.push(new Checkbox());

//access like this
checkBoxes[69].checked = true;

//loop round like this
for (var i:int = 0;i<checkBoxes.length; i ++){
   checkBoxes[i].checked = true;
}  
0
 

Author Comment

by:Didica
ID: 24033451
Hmm, i don't think that i understand your solution...

I will try to explain what i need one more time:

I have one Flex (AIR) application in which i have 70 Checkboxies, when user press one button i want to check state of those 70 checkboxies with one loop. Because of that i gave namese to those checkboxies from c1 - c70
0
 
LVL 12

Expert Comment

by:scooby_56
ID: 24037114
Are the checkboxes created using actionscript?
example
var c1:Checkbox = new Checkbox();
or at design time by draggin them onto the stage?

either way.. to loop through them you will need to insert them into an array
var checkBoxes:Array = new Array();
checkBoxes.push(c1);
checkBoxes.push(c2);
checkBoxes.push(c3); ///etc..

then you can loop through as-per the exmple i gave in the above post.

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 12

Expert Comment

by:scooby_56
ID: 24037200
Actually if you added at design time and gave instance names via the properties panel just use
for (var i=0;i<70;i++){      
     CheckBox(getChildByName('c'+(i+1))).selected= true;
}
0
 

Author Comment

by:Didica
ID: 24037433
Ok tnx, it seems that it is working. But just can you tell me one more think:

In some cases there will not always be c1 -  c70 , mybe some times there will be missing c2 or c43...
So is there a way to first check if checkbox with specific ID exists and then to manipulate it.

If i don't check it i will get   null object reference  error
0
 
LVL 12

Expert Comment

by:scooby_56
ID: 24038263
//try this

for (var i=0;i<70;i++){    
 if(getChildByName('c'+(i+1)) ){
     CheckBox(getChildByName('c'+(i+1))).selected= true;
  }
}
0
 

Author Comment

by:Didica
ID: 24038477
Tnx, that is great.

And if it is not big problem for you, can you please tell me how following line will looklike for chechbox that is located in component file (which i include in main file and give it id="CompCheckBox")

 CheckBox(getChildByName('c'+(i+1))).selected


in "noraml" - manual access to each checkbox i would use
CompCheckBox.c1.selected .....

Tnx in advance!
0
 
LVL 12

Accepted Solution

by:
scooby_56 earned 500 total points
ID: 24038883
//here you go... 3 for the price of 1 ;o)

CheckBox(CompCheckBox.getChildByName('c'+(i+1))).selected

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question