控制集合问题 [英] Controls Collection Problem

查看:57
本文介绍了控制集合问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Controls Collection更改面板(panel1)上几个标签的背景颜色.以下代码实际上可以做到这一点.所有标签均已使用相同标签(lblX)进行了标签.

I am trying to use the Controls Collection to change the background color of several labels on a panel (panel1). The following code actually works to do this. All of the labels have been tagged with the same tag (lblX).

Option Infer Off ' need this to use collections to change controls

Private Sub ChangeColor
        Dim ctrl As Control
        For Each ctrl In Controls
            If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
                Dim nestedControl As Windows.Forms.Control
                For Each nestedControl In ctrl.Controls
                    If nestedControl.Tag = "lbl1Pos" Then
                        nestedControl.BackColor = Color.Yellow
                    End If
                Next
            End If
        Next
End Sub



但是,如果我将标签放在panel1顶部的第二个面板(panel2)中,则代码不会更改标签的背景色.
关于需要什么来访问嵌套在第二个面板中的嵌套控件的任何想法?

使用VB6中可用的控制阵列,这非常容易!
感谢您的帮助
Gary Vogel



But if I put the labels in a second panel (panel2) that is placed on top of panel1 the code does not change the background color of labels.
Any thoughts on what needs to be changed to access the nested controls that are nested in the second panel???

This was quite easy with the control arrays available in VB6!

Thanks for any help
Gary Vogel

推荐答案

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ctrls As ControlCollection = Me.Controls
        Dim c As Collection = New Collection

        CtrlCollection(ctrls, c, GetType(System.Windows.Forms.Label), 0)
        CtrlColor(c, Color.Black, Color.White)
        c.Clear()
        CtrlCollection(ctrls, c, GetType(System.Windows.Forms.Panel), 0)
        CtrlColor(c, Color.White, Color.Black)
    End Sub

    Sub CtrlCollection(ByRef ctrls As Control.ControlCollection, ByRef c As Collection, ByRef systype As System.Type, ByRef nLevel As Integer)

        Dim Control As Control

        For Each Control In ctrls
            If Control.GetType Is systype Then
                c.Add(New Object() {Control, nLevel})
            End If

            If Control.GetType Is GetType(System.Windows.Forms.Panel) Then
                CtrlCollection(Control.Controls, c, systype, nLevel + 1)
            End If
        Next
    End Sub

    Sub CtrlColor(ByRef c As Collection, ByRef Ca As Color, ByRef Cb As Color)

        Dim obj As Object()

        For Each obj In c
            With obj(0)
                If Math.Ceiling(obj(1) / 2) = obj(1) / 2 Then
                    .BackColor = Ca
                    .ForeColor = Cb
                Else
                    .BackColor = Cb
                    .ForeColor = Ca
                End If
            End With
        Next
    End Sub

End Class


每个面板都有自己的面板控制集合,您需要循环遍历.



Each panel has its own control collection you need to loop through that.

i.e.

for each ctr as control in panel.controls
 if typeof ctr is textbox then
  ..do your stuff here
 end if
next


尝试类似的方法.不管它有多深.

Try something like this. Doesn''t matter how deep it is.

Public Sub SetCtlColor(ByRef u_CtlColl as controlcollection)
    on error resume next

    For Each ctl as control in u_CtlColl

        if TypeOf ctl Is label AndAlso ctl.Tag = "lbl1Pos" Then

            ctl.BackColor = Color.Yellow

        ElseIf TypeOf ctl Is panel Then

            SetCtlColor(ctl.Controls)

        End If

    Next

End Sub


这篇关于控制集合问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆