phil652
phil652

Reputation: 1506

How can I change the value of all Labels in a loop

I'm trying to change the value of my label to some value located in an array. I have a loop that goes through my array

I want to be able to do something like this

Label & i.text = "some text"

Dim QuestionArray(10) As String
'dr is OleDbDataReader from database
While dr.Read()
QuestionArray(cnt)=(dr("Question").ToString)
cnt+=1
End While

For i = 0 To QuestionArray.Length
'Label(i) can not be used
Label(i).text = QuestionArray(i)
Next i 

Upvotes: 1

Views: 2147

Answers (2)

Blackwood
Blackwood

Reputation: 4534

You can create an array to hold your labels.

Dim LabelArray() As Label = {Label0, Label1, Label2, Label3, Label4, Label5, _
  Label6, Label7, Label8, Label9, Label10}
    For i = 0 To QuestionArray.Length - 1
        LabelArray(i).Text = QuestionArray(i)
    Next

Upvotes: 1

Mohamed Jaleel Nazir
Mohamed Jaleel Nazir

Reputation: 5821

i hope it helps you...

For Each objCtrl As Control In yourFormName.Controls
        ' Assign Some Text 
If TypeOf objCtrl Is Label Then
End If
Next

Upvotes: 1

Related Questions