for each on multiple collections

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
 
P

Phill W.

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.
 
K

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
 
O

Oenone

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]
///
 
K

KW

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]
///
 
O

Oenone

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.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top