Results 1 to 7 of 7

Thread: Simple loop question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Simple loop question

    If I have a bunch of checkboxes and all I want to do is loop through them all and see if they are checked or not and depending on whether they are or not, set a variable. Here is the code that does it the long way:
    Code:
     if (chk1.Checked)
            {
                intQ1 = 1;
            }
            else
            {
                intQ1 = 0;
            }
            if (chk2.Checked)
            {
                intQ2 = 1;
            }
            else
            {
                intQ2 = 0;
            }
            if (chk3.Checked)
            {
                intQ3 = 1;
            }
            else
            {
                intQ3 = 0;
            }
            if (chk4.Checked)
            {
                intQ4 = 1;
            }
            else
            {
                intQ4 = 0;
            }
            if (chk5.Checked)
            {
                intQ5 = 1;
            }
            else
            {
                intQ5 = 0;
            }
            if (chk6.Checked)
            {
                intQ6 = 1;
            }
            else
            {
                intQ6 = 0;
            }
            if (chk7.Checked)
            {
                intQ7 = 1;
            }
            else
            {
                intQ7 = 0;
            }
    I am not sure how I would set all the variables. Each intQ is a int variable.
    I know the loop is like this but the variable is what I am stuck on.

    Code:
    foreach (Control c in this.Controls)
    {
    	if (c is CheckBox) {
    		MessageBox.Show(c.Name);
    	}
    }
    Thanks for any help

  2. #2
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: Simple loop question

    So are you saying that the only thing in this.Controls are the checkboxes? You don't even need to do an if statement. I believe this would work:

    Code:
    intQ1 = (int)chk1.Checked
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Simple loop question

    Yes but I have alot of checkboxes on some of these webpages. I was thinking something like:
    Code:
            foreach (Control ctrl in this.Controls)
            {
    
    
                if (ctrl is CheckBox)
                {
    
                    CheckBox chk = new CheckBox();
                    chk = (CheckBox)ctrl;
                    if (chk.Checked)
                    {
    
                    }
                }
            }
    but have no idea on how to have the variable match the current checkbox. With your idea I would still need a line for each checkbox. I want it not to matter how many.

  4. #4
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: Simple loop question

    First of all you're making that way too complicated... I think you could just do this:

    Code:
    foreach (Control ctrl in this.Controls)
            {
    
    
                if (ctrl is CheckBox)
                {
                    if (((CheckBox)ctrl).Checked)
                    {
    
                    }
                }
            }
    As far as the variables could you just use an array and have them correspond like that?
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Simple loop question

    Yes either would work, I think but as far as the array, I am hoping for an example, I have not really used them before and have no idea what the code would look like to make it work.

  6. #6
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: Simple loop question

    I haven't really tested this but....

    Code:
                private const int MAX_SIZE = 10;            
                CheckBox[] cbArr = new CheckBox[MAX_SIZE];
                int[] intQ = new int[MAX_SIZE];
    
                // Update the integer values
                for(int i = 0; i<MAX_SIZE; i++)
                {
                    intQ[i] = System.Convert.ToInt32(cbArr[i].Checked);
                }
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

  7. #7
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: Simple loop question

    Does this make sense? Each index in the integer(intQ) array would correspond to the same index in the CheckBox(cbArr) array. If it doesn't make sense just let me know and I can try to explain it better.
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

Posting Permissions

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



Click Here to Expand Forum to Full Width