The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Page 1 of 2 12 LastLast
Results 1 to 15 of 25

Thread: checkbox component

  1. #1
    pom's Avatar
    7,299
    posts
    Senior Member

    checkbox component

    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
    Last smoke by Ilyas : yesterday at 11:45 PM.

  2. #2
    How are ya?

    what do you mean, browsing through them?

    checkboxes is the plural!
    My name is Inigo Montoya. You killed my father. Prepare to Die!

  3. #3
    pom's Avatar
    7,299
    posts
    Senior Member
    I'm just fine, except for a bad headache and total exhaustion. Thanks for asking Inigo. I hope you're fine too.

    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
    Last smoke by Ilyas : yesterday at 11:45 PM.

  4. #4
    Hmm.. never tried it, but a function wrapped around an array might work. What do you think?
    My name is Inigo Montoya. You killed my father. Prepare to Die!

  5. #5
    pom's Avatar
    7,299
    posts
    Senior Member

    HELP PLEASE !!!

    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
    Last smoke by Ilyas : yesterday at 11:45 PM.

  6. #6
    Here's what you requested. I put it together and I think it makes for cleaner code.

    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?
    My name is Inigo Montoya. You killed my father. Prepare to Die!

  7. #7
    pom's Avatar
    7,299
    posts
    Senior Member
    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
    Last smoke by Ilyas : yesterday at 11:45 PM.

  8. #8
    I understand...


    this does the same:

    Code:
    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;
    		}
    }
    Last edited by Iammontoya; July 19th, 2002 at 03:01 PM.
    My name is Inigo Montoya. You killed my father. Prepare to Die!

  9. #9
    pom's Avatar
    7,299
    posts
    Senior Member
    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 ***

    pom
    Last smoke by Ilyas : yesterday at 11:45 PM.

  10. #10
    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:
    Code:
    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...

  11. #11
    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.
    My name is Inigo Montoya. You killed my father. Prepare to Die!

  12. #12
    the array assignment i'll skip...

    there's just two parts:
    Code:
    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.
    Code:
    	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.

  13. #13
    pom's Avatar
    7,299
    posts
    Senior Member
    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
    Last smoke by Ilyas : yesterday at 11:45 PM.

  14. #14
    pom's Avatar
    7,299
    posts
    Senior Member
    One more question : I'd like to put the ckecked checkboxes in an array. Can I declare a
    Code:
    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
    Last smoke by Ilyas : yesterday at 11:45 PM.

  15. #15
    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(), a good practice.

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012