PDA

View Full Version : checkbox component



pom
July 17th, 2002, 03:23 PM
Hey guys,
Does anybody know a way of getting all the checkbox (what's the plural for box ???) that have been checked by a user, without browsing through them ?

Thanks.

pom :asian:

Iammontoya
July 17th, 2002, 03:30 PM
How are ya?

what do you mean, browsing through them?

checkboxes is the plural!

pom
July 17th, 2002, 03:38 PM
I'm just fine, except for a bad headache and total exhaustion. :pirate: Thanks for asking Inigo. I hope you're fine too. :P

When I say browsing through them, I mean check one by one if it is checked. Which is long. That's why I wondered if there wouldn't be a function that checkes all the checkboxES and returns all those which are checked.

pom :asian:

Iammontoya
July 17th, 2002, 03:59 PM
Hmm.. never tried it, but a function wrapped around an array might work. What do you think?

pom
July 19th, 2002, 04:07 AM
OK, this is what I've done, and it works, but there has to be a better way. I have 5 checkboxes, with 5 labels and 5 instance names that are in an array

// Array of the instace names of the checkboxes
myCheck = new Array (flash,games,beer,sport,art) ;

// Function
checkDisplay = function (component) {
j=0;
trace ("\nYou have checked:\n") ;
for (i=0;i<myCheck.length;i++) {
if ( myCheck[ i].getValue() ) {
trace (myCheck[ i].getLabel());
j++
}
}
if (!j) trace ("Nothing") ;
}
// blabla
for (i=0;i<myCheck.length;i++) {
myCheck[ i].setChangeHandler ("checkDisplay") ;
}

The thing that bothers me is that this function I made has nothing to do with the component put as a parameter. Anyone has an idea ?

pom :asian:

Iammontoya
July 19th, 2002, 12:19 PM
Here's what you requested. I put it together and I think it makes for cleaner code.



function OnChange(Fcheckbox) {
a=flash.getValue();
b=games.getValue();
c=beer.getValue();
d=sport.getValue();
e=art.getValue();

answers=["flash",a,"games",b,"beer",c,"sport",d,"art",e]

}




I put a button on the main timeline with trace(_root.answers);

what do you think?

pom
July 19th, 2002, 03:21 PM
That's not exactly what I wanted. If you test my code, you'll see that it returns the checkboxes that are actually checked WHEN YOU CHECK OR UNCHECK ANY CHECKBOX. So it's not really the same.

Thanks anyway :)

pom :asian:

Iammontoya
July 19th, 2002, 03:59 PM
I understand...


this does the same:



function OnChange(Fcheckbox) {


switch(Fcheckbox){
case flash:
trace("you clicked on Flash");
break;
case games:
trace("you clicked on games");
break;
case beer:
trace("you clicked on beer");
break;
case sport:
trace("you clicked on sport");
break;
case art:
trace("you clicked on art");
break;
}
}

pom
July 19th, 2002, 04:32 PM
Did you try that code, Inigo ? Because I don't know how to use it :)

And anyway, this will trace the checkbox you've just clicked, not all the checkboxes that are clicked, and it doesn't check if the checkboxes are unchecked...

Sorry to be a pain in the *** :ninja:

pom :asian:

sbeener
July 19th, 2002, 06:11 PM
ily,

i think the board munged your code ... it's a little strange.

i'm not familiar with components, but here's a rewrite of what i thought you meant above, it's essentially the same:


myCheck = ["flash","games","beer","sport","art"];

function checkDisplay(component) {
var j,s;
for(j in myCheck){
if(myCheck[j].getValue()) s += myCheck[j].getLabel()+"\n";
}
trace("\nYou checked:\n" + ((s.length) ? s : "Nothing"));
}

untested, maybe it works...

Iammontoya
July 19th, 2002, 11:13 PM
Sbeener....Can you please dissect that code for me. Ilya.. We can probably combine this to make it work. I was just a bit unsure where you were trying to go with it.

No problem at all, though.. I love playing with this stuff.

As far as components go.

I gave the checkbox instances names (flash, beer, sports, etc.)

In the properties panel, under ChangeHandler I put onChange

that's when the code takes over...
it basically says (I write this part for the sake of others. Not you.)

Any time one of these buttons changes...do this...

If Sbeeners' solution does not work, I'll be happy to take another stab at it.

sbeener
July 20th, 2002, 04:41 AM
the array assignment i'll skip...

there's just two parts:


function checkDisplay(component) {
var j,s;
for(j in myCheck){
if(myCheck[j].getValue()) s += myCheck[j].getLabel()+"\n";
}

for each item in myCheck, if myCheck[i] is checked, add it's label and a linebreak to s.


trace("\nYou checked:\n" + ((s.length) ? s : "Nothing"));
}

trace "You checked", then if s has a length (greater than 0) trace s, otherwise trace "Nothing".

i didn't bother with assigning the handler, i'd assume that has to be done too.

pom
July 20th, 2002, 08:01 AM
Yep. Shorter code but that's basically what I was trying to do. I could never use that for (j in that) thing, because I never know what it returns (clips, clips and buttons, objects ?, here the index of the array...) And it works great !! Except the fact that you have to remove the "" in the myCheck array.

Thanks a lot Supra and Inigo!

pom :asian:

pom
July 20th, 2002, 08:08 AM
One more question : I'd like to put the ckecked checkboxes in an array. Can I declare a
var s = new Array () ;And will I be able to use it in the next scene ? Is the var destroyed right when the function ends or can I still access it ?

pom :asian:

sbeener
July 20th, 2002, 04:23 PM
if you use var to declare a variable within a function, it will not exist outside of that function.

when using a for in loop, you'll iterate through everything within the object; variables, objects, methods ... everything.

had you defined any array prorotypes, the code i posted would iterate through them as well, unless you hid them with ASSetPropFlags() (http://chattyfig.figleaf.com/flashcoders-wiki/index.php?ASSetPropFlags), a good practice.

pom
July 20th, 2002, 04:51 PM
The main benefit of ASSetPropFlags is to allow you to add properties and methods to a Class prototype without risking the property showing up in for..in loops over instances of the class which could cause any number of problems in your script.What kind of problems ?

And that site is crazy by the way. Very interesting stuff there =)

pom :asian:

pom
July 20th, 2002, 04:59 PM
And your prototype thing works indeed ! If anybody else is interested :
myA = new Array ("1","2","3") ;
Array.prototype.tracing = function () {
trace (this[0]);
}
for (j in myA) {trace (j);}Returns :
tracing
2
1
0

pom :asian:

sbeener
July 20th, 2002, 05:31 PM
well, let's say you have an object of the calss Zoo which holds a variable number of animals, each with associatied data.

you want to write a method which will return the total number of animals in the zoo


Zoo.prototype.animalCount = function(){
var c;
for(i in this){ c++; }
return(c);
}

problem is that animalCount is iterated over in the for in loop along with any other methods defined for Zoo, so you get the wrong number of animals. then if you take that value and use it in another script, say Zoo.prototype.feedAnimals(), and you end up feeding animals that don't exist!

depending on the complexity and purpose of the scripts, this can cause major problems.

for in loops are great for trouble shooting. ever need to know just what's going on in a particular object? something like this can be helpful:


Object.prototype.showContents = function(){
for(i in this){ trace(i+":"+this[i]); };
}

pom
July 21st, 2002, 06:02 AM
Thank you. And I have to say that you explain it very well. :) Puis-je user et abuser de ces informations pour un éventuel petit article général sur l'Actionscript ? (You speak French, don't you ?)

pom :asian:

Iammontoya
July 22nd, 2002, 02:16 AM
well.. Ilya.. since you understood it so well.. can you explain it to me? Maybe it has something to do with the fact that it is 1:15 in the morning..

I have been trying to understand the whole prototype thing, perhaps that is part of my problem.

Have a good night!

:)

Beener, I know you're the Man! (or Woman!). Thanks or all your help!

pom
July 22nd, 2002, 03:41 AM
Well, Inigo, the Zoo example was pretty much as clear as it can get... if you know what a class is. I'm going to try and explain, I hope Supra won't have to correct anything.

Imagine that you have an array like I wrote in a previous post.
myA = new Array ("1","2","3") ;
count=0;
for (j in myA) {count++;}
trace ("There are "+count+" elements in myA");Everything is cool, it returns 3.

Now if you add a prototype to the object Array, it will be taken into account in the loop, hence giving a wrong result
myA = new Array ("1","2","3") ;
Array.prototype.tracing = function () {
trace (this[0]);
}
for (j in myA) {count++;}
trace ("There are "+count+" elements in myA");Big big Badaboom, it returns 4.

Which means that all the regular array methods have already been ASSetPropFlaged, so that they don't appear.

Clear ? :P

pom :asian:

pom
July 22nd, 2002, 03:44 AM
If that's not in the best of Kirupa, I don't know what is. :)

pom :asian:

sbeener
July 22nd, 2002, 05:30 AM
ily, malheursement, je ne peux pas parler le francais. je suis canadienne. ici, tout le monde apprend un peu dans l'ecole.

user et abuser to your hearts content.

inigo - just another white male i'm afraid.

pom
July 22nd, 2002, 09:47 AM
:P Excuse me but your post made me laugh: you answer in French to tell me that you don't speak french, and then you say that you are a canadienne (female), but a white male. Am I the only one to see a contradiction here?

Thanks again for all the info:), I will definitely use it (what was it again ?)

pom :asian:

sbeener
July 23rd, 2002, 05:15 AM
ha ha! genders always gave me difficulty... ; )