Add controls to my form in a loop (in VB.NET)

How...can i add controls to my form in a loop? i'm trying to figure out how i can do something similar to this vb6 code in vb.NET For i = 1 To 3 Load cmdPrev(i) cmdPrev(i).Visible = False cmdPrev(i).Left = cmdPrev(0).Left + (i * Maat) cmdPrev(i).Top = cmdPrev(0).Top Next i this is just an example.... i could need this to do with some more controls...More than 3... i know i can do this.... (from vb.net help) ' Visual Basic .NET Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click Dim textBox1 As New System.Windows.Forms.TextBox textBox1.Text = "Hello" Me.Controls.Add(textBox1) If Me.Controls.Count > 1 Then MsgBox(Me.Controls(Me.Controls.IndexOf(textBox1)).Text) End If Me.Controls.Remove(button1) End Sub But...that's not exactly what i need.... i need to add the controls in a loop.... plz help :-)

1 Reply

SO SoTTo July 18, 2002 02:46 PM UTC

Private Sub addtextboxes() Dim x, i As Integer, startpos As Integer x = 3 Dim txtCountryCur(x) As TextBox startpos = Me.txtInput.Top + (Me.txtInput.Height * 1.5) For i = 0 To x txtCountryCur(x) = New TextBox() txtCountryCur(x).Text = "test " & i txtCountryCur(x).Left = 0 txtCountryCur(x).Top = txtCountryCur(x).Height * i + startpos Me.Controls.Add(txtCountryCur(x)) Next end sub

Loader.
Up arrow icon