Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET C#4.0 .NET4 Inheritance , +
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);
               // 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; }
         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 3-Jan-13 22:41pm
Edited 3-Jan-13 22:43pm
v2

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
Aarti Meswania at 4-Jan-13 5:18am
   
you mean you was added them runtime and missing line
this.controls.add(...)?
Naerling at 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 at 4-Jan-13 6:48am
   
thanks for explanation. :)
It's good you have shared your experience and post this solution.
5+ :)
Naerling at 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
0 Maciej Los 614
1 OriginalGriff 410
2 CPallini 230
3 RyanDev 161
4 Hard_Rockz 125
0 OriginalGriff 5,784
1 Sergey Alexandrovich Kryukov 4,838
2 Peter Leow 3,009
3 Maciej Los 2,894
4 DamithSL 2,465


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2014
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