Jump to content

How to Reset All Checkboxes?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
aurakami

aurakami

    Newbie

  • Members
  • Pip
  • 7 posts
I have a program I'm making using Visual Studio 2008 in C# and it has 16 check boxes and two text boxes. What I want is to clear all the fields with just one click of a button. I know how to clear the text boxes but I don't know how to clear the check boxes. (As of right now, I just have it set to restart the application.)

I've been Google searching for hours to no avail. Any help is greatly appreciated! Thanks!

Edit:
I found I could use this code:
checkBox1.CheckState = CheckState.Unchecked;

However I have 16, is there any simpler way or do I have to do it 16 times?

#2
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
checkBox1.Checked = false;
checkBox2.Checked = false;
...
checkBox16.Checked = false;

#3
aurakami

aurakami

    Newbie

  • Members
  • Pip
  • 7 posts
Thank you, yours did work.

For future reference, I ended up using this code:
foreach (Control control in this.Controls)

            {

                if (control.GetType() == typeof(CheckBox))

                {

                    ((CheckBox)control).Checked = false;

                }

            }

It unchecks all of the check boxes.

#4
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
Ok Wiped this together, seems to work (At least for me)
It will Clear EVERY Checkbox in a form, you can modify it test if you want to clear them if needed.

        private void ClearCheckboxes()

        {

            Control.ControlCollection C = this.Controls;

            foreach(Control TestThis in C)

            {

                if (TestThis is CheckBox)

                {

                    ClearCheckbox((CheckBox)TestThis);

                }

            }

        }

        private void ClearCheckbox(CheckBox ClearIt)

        {

            ClearIt.Checked = false;

        }

    }



#5
aurakami

aurakami

    Newbie

  • Members
  • Pip
  • 7 posts
I actually got an even shorter way (thanks to a friend of mine).

foreach (CheckBox control in this.Controls.OfType<CheckBox>())

            {

                    control.Checked = false;

            }

Thanks for the help though :) Least we have a lot of options for future onlookers, haha.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users