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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15192859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:36:00  来源:igfitidea点击:

How to change value of a dynamically created Label in VB.NET?

vb.netlabeldynamically-generated

提问by Boher

In my project, a label is created for each row in the database and added to a panel control. I need a solution to achieve the following : When timer ticks, I want all those labels text values to get synced with System.Time.Now. And all those labels are named consequently. How can I access their .Text value from Time.Tick ?

在我的项目中,为数据库中的每一行创建了一个标签,并将其添加到面板控件中。我需要一个解决方案来实现以下目标:当计时器滴答作响时,我希望所有这些标签文本值与 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

And here I have problem calling those controls :

在这里,我在调用这些控件时遇到问题:

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

回答by Steven Doggart

The controls referenced by the Controlscollection property can be accessed by their name, like this:

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

However, you may find it easier to simply add them all to a list when you create them so that you can access them later. For instance, if you created a couple of lists, like this, as private fields on your form:

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

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

Then, when you create the controls, you could add them to the list, like this:

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

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

Then, when you need to loop through them, it is much easier and you don't have to be concerned with the total number that were created:

然后,当您需要遍历它们时,它会容易得多,而且您不必关心创建的总数:

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

It's worth mentioning, however, that what you are doing may be simplified quite a bit, if you added the labels to a FlowLayoutPanelcontrol instead into separate panels directly on the form.

然而,值得一提的是,如果您将标签添加到FlowLayoutPanel控件而不是直接在表单上的单独面板中,则您所做的工作可能会简化很多。

回答by new bie

Try with this code:

试试这个代码:

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