How can I dinamically add controls to my program (for example in a "For Each" statemen

Example:

Code Snippet

PublicClass Form1

Dim textbox1As TextBox

Dim button1As Button

Dim panel1As Panel

PrivateSub Button1_Click(...)

Dim arrstr()AsString = TextBox1.Text.Split(";")

Dim iAsInteger = 0

Dim lbl()As Button =Nothing

ForEach xAsStringIn arrstr

lbl(i).Name = x

lbl(i).Text = x

i += 1

Next

'panel1.Controls.Add(lbl(Whole array including layout))

EndSub

EndClass

You maybe prefer to write it in a different way, I will be happy to hear about...

thanks for reading

[1946 byte] By [ShimiWeitzhandler] at [2008-1-10]
# 1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim names() As String = TextBox1.Text.Split(";"c)
Dim buttons(names.Length - 1) As Button
For ix As Integer = 0 To names.Length - 1
buttons(ix) = New Button
buttons(ix).Name = names(ix)
buttons(ix).Text = names(ix)
buttons(ix).Location = New Point(5, ix * 35)
buttons(ix).Size = New Size(100, 30)
AddHandler buttons(ix).Click, AddressOf ButtonClick
Next
Panel1.Controls.AddRange(buttons)
End Sub
Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = CType(sender, Button)
Console.WriteLine("{0} clicked", btn.Name)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "nobugz;waz;here"
End Sub
End Class
nobugz at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...