Looping thru controls - esp. Checkbox

  • Thread starter Thread starter Graeme Anderson
  • Start date Start date
G

Graeme Anderson

I have written some code that loops through a windows panel that contains
many checkboxes. Depending on the flag, it should set the checkbox value to
true or false. The full framework has the HasChildren method, CF does not.

The code example below runs until I try and set the checkbox value then
bombs out. Any ideas appreciated.

Graeme Anderson

public static void ClearFormChecks (Control CallForm, bool CheckedValue )
{
// Iterate through each control on the panel passed in
foreach (Control ctl in CallForm.Controls )
{
// is the control a checkbox?
Type type = ctl.GetType();
if ((type.Name.ToString()) == "CheckBox" )
{
// change flag
Object t = type.GetType(); //<-- bombs here
CheckBox ChkTmp = (CheckBox)t;
ChkTmp.Checked = CheckedValue;
}
}
}
 
Hi,

Comment the following line
Object t = type.GetType(); //<-- bombs here

Instead of
CheckBox ChkTmp = (CheckBox)t;
try
CheckBox ChkTmp = (CheckBox)ctl;

Girish


Instead of
 
Try:

foreach( Control ctl in CallForm.Controls )
{
if ( ctl is CheckBox )
{
CheckBox cb = ctl as CheckBox;
cb.Checked = true;
}
}
 
Back
Top