Home All Groups Group Topic Archive Search About
Author
19 Sep 2006 12:16 PM
Hamed
Hello

I have a form that containes a lot of controls including panels those
contain other controls. Is there a way to simply get all contained controls
either directly or indirectly through other containers such as panel or
GroupBox controls?

Author
19 Sep 2006 1:11 PM
Chris Dunaway
Hamed wrote:
> Hello
>
> I have a form that containes a lot of controls including panels those
> contain other controls. Is there a way to simply get all contained controls
> either directly or indirectly through other containers such as panel or
> GroupBox controls?

Use a recursive function and start with the form:

private void button1_Click(object sender, EventArgs e)
{
   enumerateControls(this.Controls);
}

private void enumerateControls(ControlCollection container)
{
   foreach(Control c in container)
   {
      //Do something with control here

      //Now check this control to see if it has any controls of its own
      if (c.Controls.Count > 0)
         enumerateControls(c.Controls);
   }
}

Hope this helps and watch for typos
Author
19 Sep 2006 1:35 PM
rowe_newsgroups
Or for the C# impaired here's the same code in VB syntax

' Air code warning!

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        enumerateControls(Me.Controls)
    End Sub

    Private Sub enumerateControls(ByVal container As ControlCollection)
        Dim c As Control
        For Each c In container
            ' Do something with control here

            ' Now check this control to see if it has any controls of
its own
            If c.Controls.Count > 0 Then
                enumerateControls(c.Controls)
            End If
        Next
    End Sub

Thanks,

Seth Rowe


Chris Dunaway wrote:
Show quoteHide quote
> Hamed wrote:
> > Hello
> >
> > I have a form that containes a lot of controls including panels those
> > contain other controls. Is there a way to simply get all contained controls
> > either directly or indirectly through other containers such as panel or
> > GroupBox controls?
>
> Use a recursive function and start with the form:
>
> private void button1_Click(object sender, EventArgs e)
> {
>    enumerateControls(this.Controls);
> }
>
> private void enumerateControls(ControlCollection container)
> {
>    foreach(Control c in container)
>    {
>       //Do something with control here
>
>       //Now check this control to see if it has any controls of its own
>       if (c.Controls.Count > 0)
>          enumerateControls(c.Controls);
>    }
> }
>
> Hope this helps and watch for typos
Author
19 Sep 2006 1:36 PM
rowe_newsgroups
Or for the C# impaired here's the same code in VB syntax

' Air code warning!

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        enumerateControls(Me.Controls)
    End Sub

    Private Sub enumerateControls(ByVal container As ControlCollection)
        Dim c As Control
        For Each c In container
            ' Do something with control here

            ' Now check this control to see if it has any controls of
its own
            If c.Controls.Count > 0 Then
                enumerateControls(c.Controls)
            End If
        Next
    End Sub

Thanks,

Seth Rowe


Chris Dunaway wrote:
Show quoteHide quote
> Hamed wrote:
> > Hello
> >
> > I have a form that containes a lot of controls including panels those
> > contain other controls. Is there a way to simply get all contained controls
> > either directly or indirectly through other containers such as panel or
> > GroupBox controls?
>
> Use a recursive function and start with the form:
>
> private void button1_Click(object sender, EventArgs e)
> {
>    enumerateControls(this.Controls);
> }
>
> private void enumerateControls(ControlCollection container)
> {
>    foreach(Control c in container)
>    {
>       //Do something with control here
>
>       //Now check this control to see if it has any controls of its own
>       if (c.Controls.Count > 0)
>          enumerateControls(c.Controls);
>    }
> }
>
> Hope this helps and watch for typos
Author
19 Sep 2006 2:25 PM
Ignacio Machin ( .NET/ C# MVP )
Hi,

Please do not crosspost to so many groups, you have included both C# and VB
ngs.

Post to the group you think will have more in common with your problem and
post to others if not answer is received.


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Show quoteHide quote
"Hamed" <ha***@raymehr.com> wrote in message
news:uwZpJY%232GHA.1068@TK2MSFTNGP05.phx.gbl...
> Hello
>
> I have a form that containes a lot of controls including panels those
> contain other controls. Is there a way to simply get all contained
> controls either directly or indirectly through other containers such as
> panel or GroupBox controls?
>
>
>
Author
19 Sep 2006 2:43 PM
Stoitcho Goutsev (100)
Hamed,

You need to traverse the control tree; each control has Controls property
where you can find its children. There is no method that gives you all
controls in a flat list.


--
HTH
Stoitcho Goutsev (100)

Show quoteHide quote
"Hamed" <ha***@raymehr.com> wrote in message
news:uwZpJY%232GHA.1068@TK2MSFTNGP05.phx.gbl...
> Hello
>
> I have a form that containes a lot of controls including panels those
> contain other controls. Is there a way to simply get all contained
> controls either directly or indirectly through other containers such as
> panel or GroupBox controls?
>
>
>
Author
19 Sep 2006 3:05 PM
Henrik Rasmussen
Here's an example in C# using an iterator. It'll return all controls in their
front to back order (z-order, not tab-order):

public static IEnumerable<Control> GetAllControls(Control parent)
{
    foreach (Control child in parent.Controls)
    {
        yield return child;
        foreach (Control grandChild in GetAllControls(child))
            yield return grandChild;
    }
}

and you can use it like this:

foreach (Control c in GetAllControls(form))
    Trace.WriteLine(c.Name);

Hope this helps.

Regards
Henrik


Show quoteHide quote
"Hamed" wrote:

> Hello
>
> I have a form that containes a lot of controls including panels those
> contain other controls. Is there a way to simply get all contained controls
> either directly or indirectly through other containers such as panel or
> GroupBox controls?
>
>
>
>