Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
5.00/5 (1 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:
C#
public void LoopThroughControls(Control parent)
{
   if (parent != null)
   {
      foreach (Control ctrl in parent.Controls)
      {
         this.LoopThroughControls(ctrl);
         // 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):
C#
public override ISite Site
{
   get { return base.Site; }
   set
   {
      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
Updated 3-Jan-13 22:43pm
v2

1 solution

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.
 
Share this answer
 
Comments
Aarti Meswania 4-Jan-13 5:18am    
you mean you was added them runtime and missing line
this.controls.add(...)?
Sander Rossel 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+ :)
Sander Rossel 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900