Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Controls for a form

Status
Not open for further replies.

roccorocks

Programmer
Nov 26, 2002
248
US
Need to get ALL the controls on a form. I know you are saying this is easy, well, when I say all, I really mean all (menu lists, nested trees, ect.). Currently I can grab all the top most using the Controls collection from the form. However, I need all the children as well (and childrens childrens, so on). Is there somewhere besides the control collection that I can grab "controls" list? Is there an assembly manifest with everything in it (kinda like in the form designer and the InitializeComponent())? Any help is appreciated.

Rocco
 
use recursion
Code:
public IEnumerable<Controls> recurse_through_controls(Control control)
{
   return control.SelectMany(child => recurse_through_controls(child);
}
it's off the top of my head, so there may be errors. also this method will not return the root control that you start from. but that's easy enough to overcome.
Code:
var ctrls = new List<Control>();
ctrls.Add(this);
ctrls.AddRange(recurse_through_controls(this));

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I will see what i can do with this, though i did figure out another solution. One thing thoug, I did not see the method SelectMany as part of control, is that a typo?
 
SelectMany is an extension method (.net 3.0) of IEnumerable<T>. looking at my example the syntax is incorrect because control doesn't implement IEnumerable<Control>. However control.Controls does.

the same could have been done this way
Code:
public IEnumerable<Controls> recurse_through_controls(Control control)
{
    var list = new List<Controls>();
    list.Add(control);
    foreach(var child in control.Controls)
       list.AddRange(recurse_through_controls(child);
    return list;
}
this would also take care of loading the root control. the client consuming this member would now be very simple

Code:
var ctrls = recurse_through_controls(this);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks again for the help Jason. I am still working with the 2.0 framework still. I made some adjustments and the code is fine. However, it did not pull the childs for my MenuStrip (which have pretty much the same properties of the parent). Doing a simple do loop on the each control will get the same result set also (which I already had in place). I guess I should have given the example of the MenuStrip (and maybe my explanation was not sufficient). I currently have a method that does some recursion to drill down the menustrip, but as you can imagine, that is not very efficient coding (just did it for my test to see if I could pull all of the children). I tried using reflection to generisize this process, but that did not turn out as I wanted either. I will get back.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top