Create controls at runtime

T

TonyMast

VB 2005 - XP Pro - Windows forms
I'm trying to write a simple multiplication tables program for my little
girl.
How can I create 200 labels, 100 textboxes.

Here is what I've tried so far (without luck).
For looper As Int16 = 1 To 101

Dim lab As New Label


lab.Name = "labQuestion" & Format(looper)

Me.Controls.Add("labQuestion" & Format(looper)) 'seems I can only add them
one at a time and it doesn't recognize labquestion1 ...

Next

'Me.Controls.Add(lab)

Thanks for any help
 
S

Scott M

You should probably set WHERE you want the labels to go on the form. Also,
pass the object, not the name, into the add method.
 
R

R. MacDonald

Hello, Tony,

In addition to Scott's advice, don't forget to also set the Size of the
controls. (Actually, you will probably want to do this first, as it
will likely influence where the control will sit.) You could modify the
code you already have by adding a few lines to make it something like:

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load

For looper As Int16 = 1 To 101

Dim lab As New Label

lab.Height = 16 ' Set Size.
lab.Width = 150

lab.Left = 20 ' "...set WHERE".
lab.Top = 20 + (looper - 1) * (lab.Height + 4)

lab.Name = "labQuestion" & Format(looper)
lab.Text = "I am label # " & looper

Me.Controls.Add(lab) ' "pass the object, not the name"

Next

End Sub

Cheers,
Randy
 
T

TonyMast

Thanks to both, but I'm still stuck on how to use the
me.controls.add
Do I have to do this 300 times to create 300 controls?
I wanted to use some loops to create the objects.
I wanted to create objects with a for next loop,
for x =1 to 100
object = "label" & format (X)
dim object as new label
'I would also add the sizes and location here
next

Just can't figure out how to do it.

Thanks
Tony
 
R

R. MacDonald

Hello, Tony,

Ummm... Maybe a good night's sleep and then a fresh look through the
example in the morning will help. :)

Try pasting the example exactly as it is into the default Form created
when you create a new project. Then hit the F5 key.

Cheers,
Randy
 
T

TonyMast

Sorry, for some reason it wasn't working in my form. I copied the example
exactly to the form load and it didn't work. However I created a new form
and it works fine. Very strange why it wasn't working in the first form but
when I created another it did.

Thanks and again sorry
Tony
 
R

R. MacDonald

Hello, Tony,

Hey, nothing to be sorry about. Things like that happen to us all.

Quite seriously, when things stop making any sense at all, I find it is
helpful to just step back for a breather and focus on something else for
a while. This is particularly true if you start saying to yourself "I
can't...". Our minds, being the amazing things that they are will try
to protect us by making us right. Hence, when you think you can't, you
find out that you're right, you can't...

But when you come back to the problem with a different perspective, i.e.
"I can..." or "I will..." often things work much better and begin to
make sense again.

Keep at it, and good luck.

Cheers,
Randy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top