each control in form
-
hello, i have 5 checkboxes in the form, i want to do something like this:
foreach (control c in this.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } }
the problem is: if the checkboxes are inside a panel, ou a groupbox, i cant verify if exists checkboxes in the form... what i can do to find the checboxes that are inside de panel/groupbox? thanks a lot -
hello, i have 5 checkboxes in the form, i want to do something like this:
foreach (control c in this.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } }
the problem is: if the checkboxes are inside a panel, ou a groupbox, i cant verify if exists checkboxes in the form... what i can do to find the checboxes that are inside de panel/groupbox? thanks a lotControls can have child controls of their own. If you know the checkboxes are in a group or on a panel, all you have to do is look in the Controls collection of that container control. Also, don't EVER convert the type name to a string and compare it to what you think the full type name should be. You'll just end up writing code where you can't fathom what wrong with it.
foreach (control c in Panel1.Controls) if (c is CheckBox) ...
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
hello, i have 5 checkboxes in the form, i want to do something like this:
foreach (control c in this.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } }
the problem is: if the checkboxes are inside a panel, ou a groupbox, i cant verify if exists checkboxes in the form... what i can do to find the checboxes that are inside de panel/groupbox? thanks a lotforeach (control c in Panel.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } }
only two letters away from being an asset
-
hello, i have 5 checkboxes in the form, i want to do something like this:
foreach (control c in this.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } }
the problem is: if the checkboxes are inside a panel, ou a groupbox, i cant verify if exists checkboxes in the form... what i can do to find the checboxes that are inside de panel/groupbox? thanks a lotcheck the type of c and if it is a panel use the c.Controls to itenerate through it's children. -- modified at 16:21 Monday 21st May, 2007
topcoderjax
-
check the type of c and if it is a panel use the c.Controls to itenerate through it's children. -- modified at 16:21 Monday 21st May, 2007
topcoderjax
Recurse the controls as a tree. Call seekCheck pasing the form as first control. private void seekCheck (Control B) { foreach(Control Con in B.Controls) { if(Con.Controls != null) //is a container { seekCheck (Con); } else { if(Con id CheckBox) { MessageBox ("....."); } } } }
-
hello, i have 5 checkboxes in the form, i want to do something like this:
foreach (control c in this.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } }
the problem is: if the checkboxes are inside a panel, ou a groupbox, i cant verify if exists checkboxes in the form... what i can do to find the checboxes that are inside de panel/groupbox? thanks a lotFernando Jaspion wrote:
if (c.GetType.ToString() == "System.Forms.CheckBox")
You don't want to do a string compare here. What you want is:
if (c.GetType() == typeof(System.Forms.CheckBox);
Ian
-
hello, i have 5 checkboxes in the form, i want to do something like this:
foreach (control c in this.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } }
the problem is: if the checkboxes are inside a panel, ou a groupbox, i cant verify if exists checkboxes in the form... what i can do to find the checboxes that are inside de panel/groupbox? thanks a lotHello, I mostely prefere to cast direct to the class I'm checking on, cause often a member of this class is on interest.
CheckBox cb = c as CheckBox;
if(cb!=null)A recursive method would check the "Controls" property of the Control instance c.
RecursiveMethod(this.Controls);
private void RecursiveMethod(ControlsCollection cc)
{
if(cc!=null)
{
foreach(Control c in cc)
{
CheckBox cb = c as CheckBox;
if(cb!=null)
{
//Do stuff
}
else
{
RecursiveMethod(c.Controls);
}
}
}
}Hope it helps! All the best, Martin
-
Fernando Jaspion wrote:
if (c.GetType.ToString() == "System.Forms.CheckBox")
You don't want to do a string compare here. What you want is:
if (c.GetType() == typeof(System.Forms.CheckBox);
Ian
no no, just if (c is CheckBox) ... :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
no no, just if (c is CheckBox) ... :)
Luc Pattyn [My Articles] [Forum Guidelines]
right you are! :-O
Ian