Label Array in VB.net [closed]

2019-09-21 16:02发布

问题:

I have 10 labels (Label1, Label2, Label3, Label4, etc...) in an Array and I need to change the Text property with a timer, I have the timer working well, but I don't know how to change one Label at the time( this second the label1, the next second label2, the next second label3...etc)...

I'm using VB.NET with the .NET 4.0 Framework in Visual Studio.

Thanks!

回答1:

If you have the timer set up and working already, try something like this for your array:

'These will be your labels:
Dim oLabel As New Label
Dim oLabel2 As New Label

'Create the array from your labels:
Dim aLabels() As Label = {oLabel, oLabel2}

'loop through your array:
For each oLabel as Label in aLabels

  oLabel.Text = "your text value here"

Next


回答2:

You can try with something like this:

Imports System.Windows.Forms

Public Class Form1
Private _labels As Label()

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    InitalizeLabelArray()
End Sub

Private Sub InitalizeLabelArray()
    _labels = New Windows.Forms.Label() {Label1, Label2, Label3}
End Sub


End Class


回答3:

VB.NET is not VB6 and does not have control arrays.

There are ways to emulate them (adding them to a collection and looping over the collection), or using the Form.Controls collection and only acting on Label controls, for example.