如何在VB.NET中更改动态创建的Label的值?

时间:2013-03-04 00:50:06

标签: vb.net label dynamically-generated

在我的项目中,为数据库中的每一行创建一个标签,并将其添加到面板控件中。我需要一个解决方案来实现以下目标: 当计时器滴答时,我希望所有这些标签文本值与System.Time.Now同步。所有这些标签都是这样命名的。如何从Time.Tick中访问他们的.Text值?

For i = 1 To ds.Tables("MyTable").Rows.Count
  Dim NextPanel As New Panel
  Dim NextLabel As Label
  NextPanel.Controls.Add(NextLabel)
  MyForm.Controls.Add(NextPanel)
  NextLabel.Name = "MyLabel" & i
  NextPanel.Name = "MyPanel" & i
Next

在这里我有问题调用这些控件:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' I want each label's text to be Time.Now()
End Sub

2 个答案:

答案 0 :(得分:4)

Controls集合属性引用的控件可以通过其名称访问,如下所示:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    For i As Integer = 1 to ds.Tables("MyTable").Rows.Count
        Dim panel As Panel = CType(MyForm.Controls("MyPanel" & i.ToString()), Panel)
        Dim label As Label = CType(panel.Controls("MyLabel" & i.ToString()), Label)
    Next
End Sub

但是,您可能会发现在创建列表时将它们全部添加到列表中更容易,以便以后可以访问它们。例如,如果您创建了几个列表(如下所示)作为表单上的私有字段:

Private myPanels As New List(Of Panel)()
Private myLabels As New List(Of Label)()

然后,当您创建控件时,可以将它们添加到列表中,如下所示:

For i = 1 To ds.Tables("MyTable").Rows.Count
  Dim nextPanel As New Panel()
  Dim nextLabel As New Label()
  nextPanel.Controls.Add(nextLabel)
  MyForm.Controls.Add(nextPanel)
  nextLabel.Name = "MyLabel" & i.ToString()
  nextPanel.Name = "MyPanel" & i.ToString()

  'Add them to the lists
  myPanels.Add(nextPanel)
  myLabels.Add(nextLabel)
Next

然后,当你需要遍历它们时,它会更容易,你不必担心创建的总数:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    For Each label As Label In myLabels
        ' ...
    Next
End Sub

但是,值得一提的是,如果您将标签添加到FlowLayoutPanel控件而不是直接在表单上的单独面板中,那么您正在做的事情可能会有所简化。

答案 1 :(得分:2)

尝试使用此代码:

Public Class Form1

Private panelList As New List(Of Panel)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    For i = 1 To ds.Tables("MyTable").Rows.Count
        Dim newPanel As New Panel
        newPanel.Name = "MyPanel" & i
        newPanel.Size = New Size(150, 22)
        newPanel.BackColor = Color.Yellow

        If (i = 1) Then
            newPanel.Location = New Point(10, 10)
        Else
            newPanel.Location = New Point(10 * i + ((i - 1) * newPanel.Width), 10)
        End If

        Dim newLabel As New Label
        newLabel.Name = "MyLabel" & i
        newLabel.Dock = DockStyle.Fill
        newPanel.Controls.Add(newLabel)

        Me.Controls.Add(newPanel)

        // save panel in generic list
        panelList.Add(newPanel)
    Next

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    // read panel from generic list
    For Each panel In panelList
        For Each control In panel.Controls
            If TypeOf (control) Is Label Then
                control.Text = control.Name + ": " + DateTime.Now.ToString("dd/mm/yy hh:MM:ss")
            End If
        Next
    Next
End Sub

End Class