Click here to Skip to main content
15,885,435 members

Looping through Controls in inherited Form...

Sander Rossel asked:

Open original thread
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?
Tags: C# (C# 4.0), .NET (.NET4), Windows Forms, Inheritance

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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