通过不在VB中工作的组件递归循环

时间:2011-04-27 21:32:14

标签: vb.net loops recursion

我正在尝试以递归方式遍历窗口中的组件,但它永远不会通过窗口到达其子组件。我做错了什么?

Public Sub fixUIIn(ByRef comp As System.ComponentModel.Component, ByVal style As SByte)
    Debug.WriteLine(comp)
    If TypeOf comp Is System.Windows.Forms.ContainerControl Then
        Dim c As System.Windows.Forms.ContainerControl
        c = comp
        c.BackColor = getColor(style, PART_BACK)
        c.ForeColor = getColor(style, PART_TEXT)
        If ((comp.Container IsNot Nothing) AndAlso (comp.Container.Components IsNot Nothing)) Then
            For i As Integer = 0 To comp.Container.Components.Count() Step 1
                fixUIIn(comp.Container.Components.Item(i), style)
            Next
        End If
        comp = c
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

不确定为什么要从Component而不是Control开始,但是如果你可以从Control(例如表单)开始,你可以

Public Sub fixUIIn(ByRef comp As System.Windows.Forms.Control ByVal style As SByte)
    Debug.WriteLine(comp)
     comp.BackColor = getColor(style, PART_BACK)
     comp.ForeColor = getColor(style, PART_TEXT)

        If (comp.Controls IsNot Nothing) Then
            For i As Integer = 0 To comp.Controls.Count() 
                fixUIIn(comp.Controls.Item(i), style)
            Next
        End If

End Sub