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?
4 replies to this topic
#1
Posted 19 July 2011 - 12:48 PM
|
|
|
#2
Posted 19 July 2011 - 01:17 PM
checkBox1.Checked = false;
checkBox2.Checked = false;
...
checkBox16.Checked = false;
checkBox2.Checked = false;
...
checkBox16.Checked = false;
#3
Posted 19 July 2011 - 01:29 PM
Thank you, yours did work.
For future reference, I ended up using this code:
It unchecks all of the check boxes.
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
Posted 19 July 2011 - 01:31 PM
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.
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
Posted 19 July 2011 - 01:40 PM
I actually got an even shorter way (thanks to a friend of mine).
Thanks for the help though :) Least we have a lot of options for future onlookers, haha.
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


Sign In
Create Account

Back to top









