So I'm having a relatively simple task of looping through all
Controls
on a
Form
, something I've done many times before without troubles. This time I do have a problem though. The
Form
I'm passing to my function has a base
Form
which already has some
Controls
on it. Now the problem is that my function only loops through the
Controls
on the base
Form
, ignoring all the
Controls
I added to the current
Form
.
The function I'm using to loop through the
Controls
on my
Form
looks like this:
public void LoopThroughControls(Control parent)
{
if (parent != null)
{
foreach (Control ctrl in parent.Controls)
{
this.LoopThroughControls(ctrl);
}
}
}
I use this from within a
Component
and I get my
Form
as follows (copied from the
System.Windows.Forms.ErrorProvider Site Property
):
public override ISite Site
{
get { return base.Site; }
set
{
base.Site = value;
if ((value != null))
{
IDesignerHost host = value.GetService(typeof(IDesignerHost)) as IDesignerHost;
if ((host != null))
{
IComponent rootComponent = host.RootComponent;
if (rootComponent is ContainerControl)
{
this.ContainerControl = (ContainerControl)rootComponent;
}
}
}
}
}
I then call the
LoopThroughControls
function passing in
ContainerControl
as parameter. When I set a breakpoint I can see all the
Controls
in
ContainerControl
, but they are ignored when looping through the
Controls
.
When I call the above function directly from my derived
Form
is DOES loop through ALL
Controls
. However,
ContainerControl
seems to have a reference to the derived
Form
as well, so I can't really see the difference...
So anyone has an explanation, and even better, a solution?