Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

C# Windows Form - loop through all controls in controls collection for the form

I am attempting to create a UserControl for a Windows Form in order to accomplish the following:

-The design surface of the user control only has a checkbox control on it.
-In the code-behind I added an Interface:

 public partial class CodeGenCheckBox : UserControl, ICodeGenCheckBox

The interface is functioning as a sort of "tag" for the control, so that during run time type identification I can gather ALL the checkbox controls of type ICodeGenCheckBox into a List<  >  of type ICodeGenCheckBox, then loop through that list and do work.

Does the above approach sound workable?

Assuming for the moment that it is, what I am asking for is a method that I can call recursively that will iterate through ALL the controls and sub controls and sub sub controls (as deep as they are nested) on the form and in that recursive method call I want to add some logic that essentially says:  "if the control you are looking at is of type ICodeGenCheckBox, then add the control to the List<ICodeGenCheckBox>"

I know I've asked a similar question before, but I can't find it in my knowledge base (yet).  If I find it I'll come back here and post.

Solution code needs to be in C# for points on this question.
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tom Knowlton

ASKER

Is the call:

 getControls(this.Form);

or

 getControls(this);

???

this.Form   does not appear to be available...
whatever your top control is where you want to search down from.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The Interface adds nothing except a little more work for the compiler, and possibly some kind of puzzlement for programmers who see it.

Yep, you're right, I don't need it.

Here is my current (final?) version:

 private void buttonGenerateCodeBasedOnSelections_Click(object sender, EventArgs e)
        {   
            textBoxResults.Text += "";

            getControls(this);

            foreach (DevHelper.CodeGenCheckBox cgcb in _CodeGenCBList)
            {
                CodeGenTypes cg = cgcb.CGType;

                switch(cg)
                {
                    case CodeGenTypes.MemberVariablesPrivate:
                        textBoxResults.Text += _CG.GeneratePrivateMemVars();
                        break;
                    case CodeGenTypes.Accessors:
                        textBoxResults.Text += _CG.GenerateAccessors();
                        break;
                    default:
                        textBoxResults.Text += "";
                        break;
                }               
            }
        }

        private void getControls(System.Windows.Forms.Control c)
        {
            if (c.GetType().ToString().Contains("CodeGenCheckBox"))
            {
               
                if (((CodeGenCheckBox)c).Checked)
                {
                    _CodeGenCBList.Add((CodeGenCheckBox)c);
                }               
            }

            foreach (Control ctrl in c.Controls)
            {
                getControls(ctrl);
            }
        }

Open in new window

NOTE:

Never at any time did I see this.Form as an option during Intellisense code completion.

But just passing "this" into the recursive method seems to work fine.
That's because the way you originally worded the problem, it sounded like you were running the code from within the UserControl itself.

It should actually be the .ParentForm() property:
https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(System.Windows.Forms.ContainerControl.ParentForm);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5.2);k(DevLang-csharp)&rd=true

If you're running from the Form itself then you don't need that.
That's because the way you originally worded the problem, it sounded like you were running the code from within the UserControl itself.

Oh, okay.  Thanks for the clarification!