2011-04-07 62 views
0

我试图通过使用递归方法重新着色一个窗体中的所有组件。然而,它总是重新记录表格,然后停下来。我怎样才能让它走过这一步?这里是我一直在试验的代码:在VB中递归重新着色

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 
    If TypeOf comp Is System.Windows.Forms.ButtonBase Then 
     Dim c As System.Windows.Forms.ButtonBase 
     c = comp 
     c.FlatStyle = Windows.Forms.FlatStyle.Flat 
     c.BackColor = getColor(style, PART_BOX) 
     c.ForeColor = getColor(style, PART_TEXT) 

     comp = c 
    End If 
    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 
End Sub 
+0

你运行这一点,以便补偿=当前的形式?尝试在窗体上的控件上运行它。 – Toby 2011-04-07 20:18:09

+0

是的,是怎么回事? – Supuhstar 2011-04-08 19:23:23

回答

1

你只是在重命名按钮控件和容器控件,这是有原因吗?现在的问题指出:“所有”,这就是为什么我问..

我也很讨厌被使用的旧VB6样式代码。为什么你会将颜色作为sByte而不是颜色来传递? Foreach也比for循环更容易使用索引。如果你想限制只有某些类型

Public Sub FixUIColors(control As Control, foreColor As Drawing.Color, backColor As Drawing.Color) 
    control.BackColor = foreColor 
    control.ForeColor = backColor 
    If TypeOf control Is ButtonBase Then 
     DirectCast(control, ButtonBase).FlatStyle = FlatStyle.Flat 
    End If 

    ' iterate through child controls 
    For Each item In control.Controls 
     FixUIColors(item, foreColor, backColor) 
    Next 
End Sub 

,你可以这样做:

我会写出如下这个

Select Case control.GetType 
     Case GetType(ContainerControl) 
     Case GetType(ButtonBase) 
      control.BackColor = foreColor 
      control.ForeColor = backColor 
    End Select 

我很困惑,在您使用style as SBytePART_BOXPART_TEXT等,所以我可能没有那样做你想要的。我认为这是错误的东西路过,你应该简单地传递的System.Drawing.Color(而原因就是这么像我可以阅读和容易理解的代码)。如果您需要将一堆颜色存储在单个对象中以便更容易传递,则可以创建一个类来存储这些内容。将一个字节拆分成一堆颜色是一个不同的任务,应该由不同的功能来处理。

+0

谢谢!可悲的是,我已经安装了几个操作系统,因为这个问题被问,可以非常不考这个答案:< – Supuhstar 2012-03-21 14:44:40