Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ctrls As ControlCollection = Me.Controls
Dim c As Collection = New Collection
CtrlCollection(ctrls, c, GetType(System.Windows.Forms.Label), 0)
CtrlColor(c, Color.Black, Color.White)
c.Clear()
CtrlCollection(ctrls, c, GetType(System.Windows.Forms.Panel), 0)
CtrlColor(c, Color.White, Color.Black)
End Sub
Sub CtrlCollection(ByRef ctrls As Control.ControlCollection, ByRef c As Collection, ByRef systype As System.Type, ByRef nLevel As Integer)
Dim Control As Control
For Each Control In ctrls
If Control.GetType Is systype Then
c.Add(New Object() {Control, nLevel})
End If
If Control.GetType Is GetType(System.Windows.Forms.Panel) Then
CtrlCollection(Control.Controls, c, systype, nLevel + 1)
End If
Next
End Sub
Sub CtrlColor(ByRef c As Collection, ByRef Ca As Color, ByRef Cb As Color)
Dim obj As Object()
For Each obj In c
With obj(0)
If Math.Ceiling(obj(1) / 2) = obj(1) / 2 Then
.BackColor = Ca
.ForeColor = Cb
Else
.BackColor = Cb
.ForeColor = Ca
End If
End With
Next
End Sub
End Class