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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只重命名按钮控件和容器控件,有什么原因吗?问题指出“全部”,这就是我问的原因。
我也绝对讨厌使用旧的 VB6 样式代码。为什么要将颜色作为 sByte 传递而不是仅传递 Color? Foreach 也是一个比带有索引的 for 循环更简单的结构。
我会这样写:
如果您只想限制为某些类型,您可以这样做:
我对您使用
style as SByte
和PART_BOX
感到困惑PART_TEXT
等,所以我可能没有完全按照您想要的方式进行操作。我认为这是错误的传递方式,您应该简单地传递 System.Drawing.Color (原因是像我这样的人可以轻松阅读和理解这段代码)。如果您需要在单个对象中存储一堆颜色以使其更容易传递,那么您可以创建一个类来存储这些内容。将一个字节解包成一堆颜色是一项不同的任务,应该由不同的函数处理。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 SByte
andPART_BOX
PART_TEXT
etc, 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.