VB中的递归重新着色

发布于 2024-10-31 05:05:03 字数 1318 浏览 0 评论 0原文

我正在尝试使用递归方法对表单中的所有组件重新着色。但是,它总是对表单重新着色,然后停止。我怎样才能让它越过这一步呢?这是我一直在尝试的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2024-11-07 05:05:03

您只重命名按钮控件和容器控件,有什么原因吗?问题指出“全部”,这就是我问的原因。

我也绝对讨厌使用旧的 VB6 样式代码。为什么要将颜色作为 sByte 传递而不是仅传递 Color? 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_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:

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

If you want to restrict to only certain types, you could do something like:

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

I'm confused to your use of style as SByte and PART_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文