each control on a form

  • Thread starter Thread starter Gabriel
  • Start date Start date
G

Gabriel

Hello,

I'd like check all the control on my tabcontrol (all tabpages)

I tried this :
foreach (Control oControl in this.Controls)
{
if (oControl.GetType() == typeof(TextBox))
{
oControl.DataBindings.Clear();
}
}

But I don't pass in any case in the if and I have a lot fot texttox.

Any idea ?


Thanks,
 
I'd like check all the control on my tabcontrol (all tabpages)

I tried this :
foreach (Control oControl in this.Controls)
{
if (oControl.GetType() == typeof(TextBox))
{
oControl.DataBindings.Clear();
}
}

But I don't pass in any case in the if and I have a lot fot texttox.

That's because controls on your form can be container controls themselves
(the tab control is a container control, as is the TabPage control) and
your textboxes are probablly contained by one of these container controls
and not by the form itslef. So you must write a recursive function that
goes through the list of all the controls of your form. If Those controls
are themselves container controls, then go through the list of their
controls and so on...

E.g:

private void ClearDatabinding()
{
ClearDatabinding(this);
}

private void ClearDatabinding(Control control)
{
if (control is TextBox)
{
control.DataBindings.Clear();
}
else
{
// Go through all the controls contained by the specified control
foreach (Control c in control.Controls)
{
ClearDatabinding(c);
}
}

}
 
Back
Top