2011-04-07 61 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