By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,642 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,642 IT Pros & Developers. It's quick & easy.

Create controls at runtime

P: n/a
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
Apr 4 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

"TonyMast" <To******@xxxMSNxxx.Com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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


Apr 4 '06 #2

P: n/a
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
Scott M wrote:
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.

"TonyMast" <To******@xxxMSNxxx.Com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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

Apr 5 '06 #3

P: n/a
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. MacDonald" <sc****@NO-SP-AMcips.ca> wrote in message
news:44***********************@news.wanadoo.nl...
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
Scott M wrote:
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.

"TonyMast" <To******@xxxMSNxxx.Com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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

Apr 5 '06 #4

P: n/a
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
TonyMast wrote:
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

Apr 5 '06 #5

P: n/a
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. MacDonald" <sc****@NO-SP-AMcips.ca> wrote in message
news:44***********************@news.wanadoo.nl...
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
TonyMast wrote:
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


Apr 5 '06 #6

P: n/a
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
TonyMast wrote:
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. MacDonald" <sc****@NO-SP-AMcips.ca> wrote in message
news:44***********************@news.wanadoo.nl...
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
TonyMast wrote:

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


Apr 6 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.