I’m trying to recolor all components in a form by using a recursive method. However, it always recolors the form and then stops. How can I make it go past this step? Here is the code I’ve been experimenting with:
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
You’re only renaming button controls and container controls, is there a reason for that? The question states “all” which is why I’m asking..
I also absolutely hate the old VB6 style code being used. Why would you pass color as an sByte instead of just a Color? Foreach is also a much simpler construct to use than a for loop with indexes.
I would write this as follows:
If you want to restrict to only certain types, you could do something like:
I’m confused to your use of
style as SByteandPART_BOXPART_TEXTetc, so I probably don’t have that doing exactly what you want. I think that is the wrong thing to be passing, you should simply pass System.Drawing.Color (and the reason is so someone like me can read and understand this code easily). If you need to store a bunch of colors in a single object to make it easier to pass around, then you can make a class to store that stuff. Unpacking a byte into a bunch of colors is a different task that should be handled by a different function.