Iman B. Rad
Iman B. Rad

Reputation: 65

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

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 ?

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

Upvotes: 2

Views: 8650

Answers (2)

Steven Doggart
Steven Doggart

Reputation: 43743

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

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 FlowLayoutPanel control instead into separate panels directly on the form.

Upvotes: 4

new bie
new bie

Reputation: 3055

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

Upvotes: 2

Related Questions