Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi In this article, a method use Linq expressions. How convert this method that be usable in .net 2 without Linq?

The method:

XML
private static IEnumerable<Control> _get_all_controls(Control c)
    {
        return c.Controls.Cast<Control>().SelectMany(item =>
            _get_all_controls(item)).Concat(c.Controls.Cast<Control>()).Where(control =>
            control.Name != string.Empty);
    }
Posted
Updated 15-Aug-15 5:53am
v3
Comments
DamithSL 15-Aug-15 12:00pm    
what have you tried so far?
maysamfth 15-Aug-15 12:12pm    
i unable to work with linq unfortunatly!

You will have to look at eth documentation for each of the Linq methods, and replace them with the equivalent foreach loop.

We don't do that for you: we expect you to do at least some of the work yourself. All you have done so far (it would seem) is find some code that might do what you want, and ask us to make it do what you want.

It's pretty obvious what that code does: it returns a IEnumerable<T> collection of controls, which includes controls nested in the top level of the Controls collection.
So do it: it's only one nested loop!
 
Share this answer
 
Comments
maysamfth 16-Aug-15 2:04am    
thanks. i using list and write below code but this dont work corrctly!

private List<control> _get_all_controls(Control c)
{
List<control> lstC = new List<control>();

foreach (Control ctrl in c.Controls)
{
lstC.Add(ctrl);

_get_all_controls(ctrl);
}
return lstC;
}
OriginalGriff 16-Aug-15 2:37am    
"this dont work corrctly"
Is not a helpful error report!
What did it do that you didn't expect, or not do that you did?
maysamfth 16-Aug-15 3:46am    
Ok, i have a FORM with some controls like button, label, panel and groupbox. this code work for controls that parents are FORM and dont work for controls that parents are label or groupbox.
OriginalGriff 16-Aug-15 3:49am    
And when you say "dont work", what do you mean?
I can't see your screen, access your HDD, or read your mind! :laugh:
I only get what you type to work with here...and there are generally a lot more ways that something might not work than do what is intended.
maysamfth 16-Aug-15 4:09am    
Are you preacher?!!!
Question is clear, I want add all of controls in form and controls in panels and groupbox in a List<>.
If can help me thank you else goodbye!
This code return a list of controls in a form:

C#
private List<control> _get_all_controls(Control c)
    {
        List<control> lstC = new List<control>();

        foreach (Control ctrl in c.Controls)
        {
            lstC.Add(ctrl);

            lstC.AddRange(_get_all_controls(ctrl));
        }
        return lstC;
    }</control></control></control>
 
Share this answer
 

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