Click here to Skip to main content
12,951,732 members (64,212 online)
Rate this:
Please Sign up or sign in to vote.
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)
         // Do something with Control here...
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; }
      base.Site = value;
      if ((value != null))
         // Try to get the IDesignerHost (the Form of the current Component)
         IDesignerHost host = value.GetService(typeof(IDesignerHost)) as IDesignerHost;
         if ((host != null))
            // With the IDesignerHost it is possible to get the RootComponent.
            // This is the Form for the active designer.
            IComponent rootComponent = host.RootComponent;
            if (rootComponent is ContainerControl)
               // If the RootComponent is a ContainerControl we assign it to the ContainerControl Property.
               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?
Posted 3-Jan-13 22:41pm
Updated 3-Jan-13 22:43pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I found my own solution to this one. Sooner than expected too (although it still cost me an entire morning...).
What I was looking at were the fields of my Form, all TextBox fields, Button fields, etc.
However, they weren't placed on my Form yet, so they were also not yet added to the Controls of the Form.
I implemented ISupportInitialize and called the function at EndInit. Works as expected now.
Aarti Meswania 4-Jan-13 5:18am
you mean you was added them runtime and missing line
Naerling 4-Jan-13 6:43am
No, I mean I added all the Controls in the Winforms designer, but my function was called in InitializeComponent (the designer generated code) before all the Controls were added to the Form. The EndInit of ISupportInitialize is called at the end of InitializeComponent (after all the Controls are added to the Form) and fixed my problem.
Aarti Meswania 4-Jan-13 6:48am
thanks for explanation. :)
It's good you have shared your experience and post this solution.
5+ :)
Naerling 4-Jan-13 13:22pm
Thanks. I hate it when people find solutions to their problems, but don't tell what that solution is. I did not intend to do that ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,239
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100