轻松访问动态创建的标签

时间:2016-11-23 20:57:40

标签: vb.net

在我的程序中,我需要创建一些标签。 然而,标签的数量在很大程度上取决于许多配置文件。

标签基于加载的PLC模块类型和数量。每个PLC模块(输入模块)都有不同的I / O地址。这里唯一的常量是每个I / O地址长度为1个字(16位)。

例如,如果我加载了64个输入模块,它比需要创建的标签多4个字:

  • 0.00 ... 0.15
  • 1.00 ... 1.15
  • 2.00 ... 2.15
  • 3.00 ... 3.15

每个Label都有一个名称:

OnOff_000 ... OnOff_015等等。

但现在对我来说是棘手的部分,每个标签都使用默认的backcolor = color.lightblue进行初始化。 每个标签的背景颜色需要根据输入状态绿色(ON)和红色(OFF)进行更改。

我已设法使用以下代码更改一个标签的背景颜色:

Dim test As Label = CType(IO_ScanPanel.Controls("OnOff_000"), Label)
        test.BackColor = Color.Red

但是如何轻松地与所有其他标签一起使用

使用以下代码创建标签:

Private Function InsertControls_InputsLabels(ByVal StartAdrs As Integer, ByVal EndAdrs As Integer, ByVal LocX As Integer, ByVal LocY As Integer) As String()
    Dim NumberOfElements As Integer = 0
    Dim Coordinates(1) As String

    Do Until StartAdrs > EndAdrs

        Do Until NumberOfElements > 15
            Dim On_OffInput As Label = New Label()

            On_OffInput.AutoSize = False
            On_OffInput.Size = New Size(36, 15)
            On_OffInput.TextAlign = ContentAlignment.MiddleCenter
            On_OffInput.Location = New Point(LocX, LocY)
            If NumberOfElements < 10 Then
                On_OffInput.Text = StartAdrs & ".0" & NumberOfElements
                On_OffInput.Name = "OnOff_" & StartAdrs & "0" & NumberOfElements
            Else
                On_OffInput.Text = StartAdrs & "." & NumberOfElements
                On_OffInput.Name = "OnOff_" & StartAdrs & NumberOfElements
            End If

            On_OffInput.BorderStyle = BorderStyle.Fixed3D
            On_OffInput.BackColor = Color.LightBlue

            IO_ScanPanel.Controls.Add(On_OffInput)

            LocX = LocX + 32
            NumberOfElements = NumberOfElements + 1



        Loop
        NumberOfElements = 0
        LocY = LocY + 25
        LocX = 7
        StartAdrs = StartAdrs + 1

    Loop

    Coordinates(0) = LocX
    Coordinates(1) = LocY

    Return Coordinates

End Function

我希望有人可以帮我解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果要更改每个标签的BackColor属性,可以按如下方式迭代表单的控件集合:

For Each myControl As Control In Me.Controls
   If TypeOf(myControl) Is Label Then myControl.BackColor = Color.Red
Next

@Plutonix的建议也很好。将每个标签添加到具有表单级别范围的列表中,如下所示。我还建议使用Option Strict On编码并避免隐式类型转换。我在此基础上做了一些改变。

Private myLabels As New List(Of Label)

Private Function InsertControls_InputsLabels(ByVal StartAdrs As Integer, ByVal EndAdrs As Integer, ByVal LocX As Integer, ByVal LocY As Integer) As Integer()
    Dim NumberOfElements As Integer
    Dim Coordinates(1) As Integer

    Do Until StartAdrs > EndAdrs
        Do Until NumberOfElements > 15
            Dim On_OffInput As New Label

            On_OffInput.AutoSize = False
            On_OffInput.Size = New Size(36, 15)
            On_OffInput.TextAlign = ContentAlignment.MiddleCenter
            On_OffInput.Location = New Point(LocX, LocY)

            If NumberOfElements < 10 Then
                On_OffInput.Text = StartAdrs & ".0" & NumberOfElements
                On_OffInput.Name = "OnOff_" & StartAdrs & "0" & NumberOfElements
            Else
                On_OffInput.Text = StartAdrs & "." & NumberOfElements
                On_OffInput.Name = "OnOff_" & StartAdrs & NumberOfElements
            End If

            On_OffInput.BorderStyle = BorderStyle.Fixed3D
            On_OffInput.BackColor = Color.LightBlue

            Me.Controls.Add(On_OffInput)
            myLabels.Add(On_OffInput)
            LocX = LocX + 32
            NumberOfElements = NumberOfElements + 1
        Loop

        NumberOfElements = 0
        LocY = LocY + 25
        LocX = 7
        StartAdrs = StartAdrs + 1
    Loop

    Coordinates(0) = LocX
    Coordinates(1) = LocY

    Return Coordinates
End Function

然后可以按如下方式更新标签的BackColor属性:

For Each myLabel As Label In myLabels 
   myLabel.BackColor = Color.Red
Next