Using a Variable to Dim a Control
Posted on 2007-10-17
I would like to create a number of controls at run-time. For example, let's say I'm programming a simple game with a board that is 10x10. Visual Basic 2005 does not use control arrays like 6.0 and earlier did. You need to create each control manually or through code. Instead of drawing, editing, and placing 100 objects on the form one by one, I'd like to do this with a For...Next loop if possible.
Let's say I want to name the controls Space00, Space01, Space02... up to Space99. Here's what I have tried so far:
Dim Ctrl As New Button
Dim i As Integer
For i = 0 To 99
Select Case i
Case 0 To 9
Ctrl.Name = "Space0" & Convert.ToString(i)
Case 10 To 99
Ctrl.Name = "Space" & Convert.ToString(i)
' --- code to size and position the control ---
Ctrl.Text = Convert.ToString(i)
I used buttons for now, placing the value of i on each button, for debugging purposes. However, only the last button gets shown. What can I do to get this to do what I need?