Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 82099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:33:50+00:00 2026-05-10T21:33:50+00:00

I’ve got some code that will generically get all Controls in a form and

  • 0

I’ve got some code that will generically get all Controls in a form and put them in a list. Here’s some of the code:

        private List<Control> GetControlList(Form parentForm)         {             List<Control> controlList = new List<Control>();             AddControlsToList(parentForm.Controls, controlList);              return controlList;         }          private void AddControlsToList(Control.ControlCollection rootControls, List<Control> controlList)         {             foreach (Control c in rootControls)             {                 controlList.Add(c);                 if (c.HasChildren)                     AddControlsToList(c.Controls, controlList);                 //             }         } 

So I’m only able to use c.HasChildren to check and see if there’s any more child controls from this root control.

What about a menuStrip, toolStrip, and statusStrip? How do I get all of the controls that are in these controls generically? Ex: MenuStripItem

I know that I could try testing the c.GetType() == typeof(MenuStrip) but I was hoping to not have to do specific type tests.

If I need to give more info, please ask.

Thanks a bunch

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:33:50+00:00Added an answer on May 10, 2026 at 9:33 pm

    I believe the VS designer does it by getting an instance of the control’s designer (see the Designer attribute), and, if the designer is a ComponentDesigner, getting the AssociatedComponents property.

    EDIT:

    Okay, I guess that’s a little vague. A warning, though: what follows is a little complicated, and might not be worth the effort.

    A note on nomenclature:
    Below, I will be referring to both the designer within Visual Studio—which is the name used to refer to the functionality within Visual Studio by which the layout and content of forms and controls are edited visually—and to designer classes—which will be explained below. To prevent confusion as to which I am referring to at any given time, I will always refer to the designer functionality within Visual Studio as ‘the designer’, and I will always refer to a designer class as an ‘IDesigner’, which is the interface each must implement.

    When the Visual Studio designer loads a component (usually a control, but also things like Timer and such), it looks for a custom attribute on the class of type DesignerAttribute. (Those unfamiliar with attributes might want read up on them before continuing.)

    This attribute, if present, provides the name of a class—an IDesigner—the designer can use to interface with the component. In effect, this class controls certain aspects of the designer and of the design-time behavior of the component. There’s indeed quite a lot you can do with an IDesigner, but right now we’re only interested in one thing.

    Most controls that use a custom IDesigner use one that derives from ControlDesigner, which itself derives from ComponentDesigner. The ComponentDesigner class has a public virtual property called AssociatedComponents, which is meant to be overridden in derived classes to return a collection of references to all ‘child’ components of this one.

    To be more specific, the ToolStrip control (and by inheritance, the MenuStrip control) has a DesignerAttribute that references a class called ToolStripDesigner. It looks sort of like:

    /*  * note that in C#, I can refer to the 'DesignerAttribute' class within the [ brackets ]  * by simply 'Designer'.  The compiler adds the 'Attribute' to the end for us (assuming  * there's no attribute class named simply 'Designer').  */ [Designer('System.Windows.Forms.Design.ToolStripDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'), ...(other attributes)] public class ToolStrip : ScrollableControl, IArrangedElement, ...(other interfaces){     ... } 

    The ToolStripDesigner class is not public. It’s internal to System.Design.dll. But since it’s specified here by it’s fully qualified name, the VS designer can use Activator.CreateInstance to create an instance of it anyway.

    This ToolStripDesigner class, because it inherits [indirectly] from ComponentDesigner has an AssociatedComponents property. When you call it you get a new ArrayList that contains references to all the items that have been added to the ToolStrip.

    So what would your code have to look like to do the same thing? Rather convoluted, but I think I have a working example:

    /*  * Some controls will require that we set their 'Site' property before  * we associate a IDesigner with them.  This 'site' is used by the  * IDesigner to get services from the designer.  Because we're not  * implementing a real designer, we'll create a dummy site that  * provides bare minimum services and which relies on the framework  * for as much of its functionality as possible.  */ class DummySite : ISite, IDisposable{     DesignSurface designSurface;     IComponent    component;     string        name;      public IComponent Component {get{return component;}}     public IContainer Container {get{return designSurface.ComponentContainer;}}     public bool       DesignMode{get{return false;}}     public string     Name      {get{return name;}set{name = value;}}      public DummySite(IComponent component){         this.component = component;         designSurface = new DesignSurface();     }     ~DummySite(){Dispose(false);}      protected virtual void Dispose(bool isDisposing){         if(isDisposing)             designSurface.Dispose();     }      public void Dispose(){         Dispose(true);         GC.SuppressFinalize(this);     }      public object GetService(Type serviceType){return designSurface.GetService(serviceType);} }  static void GetComponents(IComponent component, int level, Action<IComponent, int> action){     action(component, level);      bool visible, enabled;     Control control = component as Control;     if(control != null){         /*          * Attaching the IDesigner sets the Visible and Enabled properties to true.          * This is useful when you're designing your form in Visual Studio, but at          * runtime, we'd rather the controls maintain their state, so we'll save the          * values of these properties and restore them after we detach the IDesigner.          */         visible = control.Visible;         enabled = control.Enabled;          foreach(Control child in control.Controls)             GetComponents(child, level + 1, action);     }else visible = enabled = false;      /*      * The TypeDescriptor class has a handy static method that gets      * the DesignerAttribute of the type of the component we pass it      * and creates an instance of the IDesigner class for us.  This      * saves us a lot of trouble.      */     ComponentDesigner des = TypeDescriptor.CreateDesigner(component, typeof(IDesigner)) as ComponentDesigner;     if(des != null)         try{             DummySite site;             if(component.Site == null)                 component.Site = site = new DummySite(component);             else site = null;              try{                 des.Initialize(component);                 foreach(IComponent child in des.AssociatedComponents)                     GetComponents(child, level + 1, action);             }finally{                 if(site != null){                     component.Site = null;                     site.Dispose();                 }             }         }finally{des.Dispose();}      if(control != null){         control.Visible = visible;         control.Enabled = enabled;     } }   /* We'll use this in the ListComponents call */ [DllImport('user32.dll', CharSet=CharSet.Auto)] static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);  const int WM_SETREDRAW = 11;  void ListComponents(){     /*      * Invisible controls and disabled controls will be temporarily shown and enabled      * during the GetComponents call (see the comment within that call), so to keep      * them from showing up and then disappearing again (or appearing to temporarily      * change enabled state), we'll disable redrawing of our window and re-enable it      * afterwards.      */     SendMessage(Handle, WM_SETREDRAW, 0, 0);     GetComponents(this, 0,         /* You'll want to do something more useful here */         (component, level)=>System.Diagnostics.Debug.WriteLine(new string('\t', level) + component));     SendMessage(Handle, WM_SETREDRAW, 1, 0); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Python (the language) doesn't need a GIL (which is why… May 11, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer I would go for a the bash solution. Also given… May 11, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer Depends on the project, but I've got the following for… May 11, 2026 at 8:21 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.