Home All Groups Group Topic Archive Search About

for each on multiple collections

Author
4 Oct 2006 3:15 PM
Kathy
Is there a way to do a "for each" on multiple control collections?
Something like the following which obviously doesn't work:

    for each ctl as control in form1.controls, form1.tabpage1.controls,
form2.tabpage2.controls
    next

Thanks,
KW

Author
4 Oct 2006 3:36 PM
Phill W.
Kathy wrote:
> Is there a way to do a "for each" on multiple control collections?
> Something like the following which obviously doesn't work:
>
>     for each ctl as control in form1.controls, form1.tabpage1.controls,
> form2.tabpage2.controls
>     next

Not directly, but you can loop through an /array/ of collections, then
through the Controls in each, as in :

<AirCode()> _
For Each e1 As ControlCollection _
In New ControlCollection() _
    { Form1.Controls _
    , Form1.tabPage1.Controls _
    , Form2.TabPage2.Controls _
    }
    For Each e2 As Control _
    In e1.Controls
       ' Do stuff
    Next
Next


HTH,
    Phill  W.
Author
4 Oct 2006 4:12 PM
KW
Yes!  That's what I was looking for, thanks !!
KW

Show quoteHide quote
"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> wrote in message
news:eg0kel$st5$1@south.jnrs.ja.net...
> Kathy wrote:
>> Is there a way to do a "for each" on multiple control collections?
>> Something like the following which obviously doesn't work:
>>
>>     for each ctl as control in form1.controls, form1.tabpage1.controls,
>> form2.tabpage2.controls
>>     next
>
> Not directly, but you can loop through an /array/ of collections, then
> through the Controls in each, as in :
>
> <AirCode()> _
> For Each e1 As ControlCollection _
> In New ControlCollection() _
>    { Form1.Controls _
>    , Form1.tabPage1.Controls _
>    , Form2.TabPage2.Controls _
>    }
>    For Each e2 As Control _
>    In e1.Controls
>       ' Do stuff
>    Next
> Next
>
>
> HTH,
>    Phill  W.
Author
4 Oct 2006 5:52 PM
KW
Ooops, I spoke too soon.  It sounded good, but I can't get it to work.  To
start with, the ControlCollection in "For Each e1 As ControlCollection"   is
not recognized.  I need a little more help please.
Thanks.
KW

Show quoteHide quote
"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> wrote in message
news:eg0kel$st5$1@south.jnrs.ja.net...
> Kathy wrote:
>> Is there a way to do a "for each" on multiple control collections?
>> Something like the following which obviously doesn't work:
>>
>>     for each ctl as control in form1.controls, form1.tabpage1.controls,
>> form2.tabpage2.controls
>>     next
>
> Not directly, but you can loop through an /array/ of collections, then
> through the Controls in each, as in :
>
> <AirCode()> _
> For Each e1 As ControlCollection _
> In New ControlCollection() _
>    { Form1.Controls _
>    , Form1.tabPage1.Controls _
>    , Form2.TabPage2.Controls _
>    }
>    For Each e2 As Control _
>    In e1.Controls
>       ' Do stuff
>    Next
> Next
>
>
> HTH,
>    Phill  W.
Author
4 Oct 2006 6:07 PM
Oenone
KW wrote:
> Ooops, I spoke too soon.  It sounded good, but I can't get it to
> work.  To start with, the ControlCollection in "For Each e1 As
> ControlCollection"   is not recognized.  I need a little more help
> please.

I believe this syntax was only added in VS2005. If you're using VS2003 or
earlier, try replacing this with:

\\\
    Dim e1 As ControlCollection
    For Each e1 In New ControlCollection() {
        [etc]
///

--

(O)enone
Author
4 Oct 2006 6:15 PM
KW
I'm using VS2005.  Intellisense says "type ControlCollection is not defined"

Show quoteHide quote
"Oenone" <oen***@nowhere.com> wrote in message
news:dLSUg.622$L.181@newsfe4-gui.ntli.net...
> KW wrote:
>> Ooops, I spoke too soon.  It sounded good, but I can't get it to
>> work.  To start with, the ControlCollection in "For Each e1 As
>> ControlCollection"   is not recognized.  I need a little more help
>> please.
>
> I believe this syntax was only added in VS2005. If you're using VS2003 or
> earlier, try replacing this with:
>
> \\\
>    Dim e1 As ControlCollection
>    For Each e1 In New ControlCollection() {
>        [etc]
> ///
>
> --
>
> (O)enone
>
Author
4 Oct 2006 8:28 PM
Oenone
KW wrote:
> I'm using VS2005.  Intellisense says "type ControlCollection is not
> defined"

In that case try putting the class hierarchy above ControlCollection, and
use Windows.Forms.Control.ControlCollection instead.

--

(O)enone
Author
5 Oct 2006 12:26 AM
Dennis
To be sure you get all the child controls from other child controls, you will
need some type of recursion.  Try the following (untested)

'Public Sub Get_Controls(ByVal parentCtr As Control)
    '    For Each ctr As Control In parentCtr.Controls
    '        Console.Write(ctr.Name)
    '        Get_Controls(ctr)
    '    Next
    'End Sub
--
Dennis in Houston


Show quoteHide quote
"Kathy" wrote:

> Is there a way to do a "for each" on multiple control collections?
> Something like the following which obviously doesn't work:
>
>     for each ctl as control in form1.controls, form1.tabpage1.controls,
> form2.tabpage2.controls
>     next
>
> Thanks,
> KW
>
>
>