How to have a loop to assign value to controls?

danyeungw

Well-known member
Joined
Aug 30, 2005
Messages
73
Programming Experience
10+
If there are 5 Label controls on the form and I want to put text on the labels, how do I do a loop?

Fox example,

Label1.text = function1
Lavel2.text = function1
Lavel3.text = function1
Lavel4.text = function1
Lavel5.text = function1

I would like to have a loop, like

for i = 1 to 5
...
next

What is the syntex? Thanks.

DanYeung
 
You'd have to put the Labels in an array:
VB.NET:
Dim labels As Label() = {Label1, Label2, Label3, Label4, Label5}
Dim text As String = function1()

For Each lbl As Label in labels
    lbl.Text = text
Next lbl
Notice the fact that I called function1 only once as well, which is what you should do if it's going to return the same value every time anyway.
 
Back
Top