Code repeating

Nauris

New member
Joined
Sep 24, 2007
Messages
4
Programming Experience
Beginner
Hi.

Maybe somebody can help me, with such a thing.. how to repeat code by changing object name. It could be hard to understand ;)
I mean:
VB.NET:
 Dim Label1 As New Label()
 Me.Controls.Add(Label1)

 Dim Label2 As New Label()
 Me.Controls.Add(Label2)

 Dim Label3 As New Label()
 Me.Controls.Add(Label3)

 ' ...4..5..

But if i need 2000 of them..
VB.NET:
Dim x as integer
for x = 1 to 2000
 Dim Label<<<here x value>>> As New Label()
 Me.Controls.Add(Label<<<here x value>>> )
Next

Or maybe there is another, easier way to build dynamic label field?!
 
you don't need the "x", but name the controls (Name property), makes it easier to find them again if you need to.
VB.NET:
        For x As Integer = 1 To 20
            Dim l As New Label
            l.Name = "label" & x.ToString()
            'set other properties also like location etc
            Me.Controls.Add(l)
        Next
VB.NET:
Me.Controls("label1").Text = "hello" '<--- Name is used to get the control here
 
Back
Top