for each on multiple collections

  • Thread starter Thread starter Kathy
  • Start date Start date
K

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
 
Kathy said:
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.
 
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
 
KW said:
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]
///
 
I'm using VS2005. Intellisense says "type ControlCollection is not defined"

Oenone said:
KW said:
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]
///
 
KW said:
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.
 
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
 
Back
Top