-1

嘿,我正在为我的表单创建一些运行时标签:

    Dim tmpLbl As New Label
    Dim intX As Integer = 0

    Do Until intX = 4
        With tmpLbl
            .Size() = New System.Drawing.Size(58, 15)
            .Text = "Run-time Controls " & intX
            .Location = New System.Drawing.Point(2, 20)
            .Name = "test" & intX
        End With

        With Me.Controls
            .Add(tmpLbl)
        End With

        intX += 1
    Loop

这工作得很好......但是当我想要时,说在表单上放置一个按钮,调用这些运行时控件之一,如下所示:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Debug.Print(tmpLbl.Name)
End Sub

当然,上面的这个是行不通的。当我单击该按钮时,如何获取标签名称(例如:test2)并阅读其文本(例如:运行时控件 2 )?

4

1 回答 1

0

知道了!

Private genTmpLbl As New List(Of Label)

Public Sub makeRunTimeControls()
    Dim tmpLbl As New Label
    Dim intX As Integer = 0

    Do Until intX = 4
        With tmpLbl
            .Size() = New System.Drawing.Size(58, 15)
            .Text = "Run-time Controls " & intX
            .Location = New System.Drawing.Point(2, 20)
            .Name = "test" & intX
        End With

        With Me.Controls
            .Add(tmpLbl)
        End With

        genTmpLbl.Add(tmpLbl)
        intX += 1
    Loop
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    For Each label In genTmpLbl
        For Each control In Me.Controls
            If TypeOf (control) Is Label Then
                Debug.Print(control.Name)
            End If
        Next
    Next
End Sub
于 2013-07-16T19:59:08.900 回答