7 Replies - 305 Views - Last Post: 21 May 2012 - 11:55 AM Rate Topic: -----

#1 tycox94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-May 12

For Looping Objects (CheckBox)

Posted 21 May 2012 - 10:49 AM

Best way to explain this is to show:
        private void button1_Click(object sender, EventArgs e)
        {
            for (int card = 0; card < 3; card++)
            {
                if (shouldBeChecked(card)) //<<< I only need to check specific check boxes; check the boxes that "shouldBeChecked"
                {
                    CheckBox(card).checked = true; //<<< card is the number of the checkbox
                }
                else
                {
                    CheckBox(card).checked = false; //<<< card is the number of the checkbox
                }
            }
        }


How would I best accomplish this?

Is This A Good Question/Topic? 0
  • +

Replies To: For Looping Objects (CheckBox)

#2 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 771
  • View blog
  • Posts: 1,097
  • Joined: 24-August 11

Re: For Looping Objects (CheckBox)

Posted 21 May 2012 - 11:14 AM

So what are you trying to accomplish? The best way to explain this is to type out what you're trying to make/accomplish and then show how you've attempted to accomplish this. Out of context, that's just some code that may or may not do something.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3792
  • View blog
  • Posts: 6,387
  • Joined: 08-June 10

Re: For Looping Objects (CheckBox)

Posted 21 May 2012 - 11:15 AM

So, as I understand it, you have a bunch of checkboxes named "CheckBox1", "CheckBox2", "CheckBox3" etc... on your form? And you want to get the values of them in a loop? Let me know if I have that right before I continue.
Was This Post Helpful? 0
  • +
  • -

#4 tycox94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-May 12

Re: For Looping Objects (CheckBox)

Posted 21 May 2012 - 11:19 AM

View PostCurtis Rutland, on 21 May 2012 - 11:15 AM, said:

So, as I understand it, you have a bunch of checkboxes named "CheckBox1", "CheckBox2", "CheckBox3" etc... on your form? And you want to get the values of them in a loop? Let me know if I have that right before I continue.

Yep, I need to loop each checkbox to find if it "shouldBeChecked". If so then check it, otherwise uncheck it.
Was This Post Helpful? 0
  • +
  • -

#5 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 771
  • View blog
  • Posts: 1,097
  • Joined: 24-August 11

Re: For Looping Objects (CheckBox)

Posted 21 May 2012 - 11:31 AM

So you're looking for help with the shouldBeChecked method?
Was This Post Helpful? 0
  • +
  • -

#6 tycox94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-May 12

Re: For Looping Objects (CheckBox)

Posted 21 May 2012 - 11:36 AM

View Posth4nnib4l, on 21 May 2012 - 11:31 AM, said:

So you're looking for help with the shouldBeChecked method?


I need to get line 7 to work:
CheckBox(card).checked = true; //<<< card is the number of the checkbox

I need help finding a way to do this because "CheckBox(card).checked" isn't possible.
Was This Post Helpful? 0
  • +
  • -

#7 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 771
  • View blog
  • Posts: 1,097
  • Joined: 24-August 11

Re: For Looping Objects (CheckBox)

Posted 21 May 2012 - 11:53 AM

You could make an array of checkboxes, and use the array index to check them.

var checkBoxArray = new CheckBox[]{CheckBox1, CheckBox2, CheckBox3};


for (var i = 0; i < 3; i++)
{
    if (shouldBeChecked(card))
    {
        checkBoxArray[i].Checked = true;
    }
    else
    {
        checkBoxArray[i].Checked = false;
    }
}



If you use an array like this, then a foreach loop would be better:

var checkBoxArray = new CheckBox[]{CheckBox1, CheckBox2, CheckBox3};


foreach (var checkBox in checkBoxArray)
{
    if (shouldBeChecked(card))
    {
        checkBox.Checked = true;
    }
    else
    {
        checkBox.Checked = false;
    }
}



I don't understand shouldBeChecked(card) from line 5 though. Is that pseudocode, or is that really a method that you're passing your counter to?
Was This Post Helpful? 1
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3792
  • View blog
  • Posts: 6,387
  • Joined: 08-June 10

Re: For Looping Objects (CheckBox)

Posted 21 May 2012 - 11:55 AM

You could do something like this:

var checkBoxes = Controls.OfType<CheckBox>();
foreach(var cb in checkBoxes)
{
    var name = cb.Name.Replace("checkBox", "");
    int index;
    if(!int.TryParse(name, out index)) continue;
    cb.Checked = ShouldCheck.Contains(index);
}



In this case, ShouldCheck is an array property that has the list of to-be-checked indexes. I'm taking the name of each checkbox on the form, removing the string "checkBox" from it (change that if you need), which should just leave a number. Then I'm parsing that into an int. If it's not a number, we skip the checkbox. Then, if the ShouldCheck array has the index in it, it'll be checked, otherwise unchecked.

I wouldn't really recommend doing things this way, but it'll work.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1