Using code to add controls

  • Thread starter Thread starter David Pick
  • Start date Start date
D

David Pick

I am currently switching from vb6 to vb.net. I know in vb6 in order to
add a control to a form such as a label you would use code like
me.controls.add("Forms.Label.1"). Could someone please tell me the
equivalent of this in vb.net. Thanks so much.

- David
 
Hi David,

You can actually just see how the designer does it for you by viewing
the designer code for the form.

Essentially, all forms have a collection called "Controls" which is
where you add instances of controls.

Eg. This code could be placed in the Form_Load event
Dim btnSave as new Button()
btnSave.Text = "Save"
' Set other properties, including x/y location and size
Me.Controls.Add(btnSave)

But really, just check out the designer code. You can play around with
your button properties on the form in design mode, then check the code
to see what it does.
In .VS2003 this code is in the region "Windows design generated code"
and in VS2005 this code is in a seperate designer file for your form (I
think something like MyForm.designer.vb).

Steven
 
Steven said:
Hi David,

You can actually just see how the designer does it for you by viewing
the designer code for the form.

Essentially, all forms have a collection called "Controls" which is
where you add instances of controls.

Eg. This code could be placed in the Form_Load event
Dim btnSave as new Button()
btnSave.Text = "Save"
' Set other properties, including x/y location and size
Me.Controls.Add(btnSave)

But really, just check out the designer code. You can play around with
your button properties on the form in design mode, then check the code
to see what it does.
In .VS2003 this code is in the region "Windows design generated code"
and in VS2005 this code is in a seperate designer file for your form (I
think something like MyForm.designer.vb).

Steven

Thanks Steven, that did just what I wanted it to. I actually do want to
add controls this because for the project I'm working on I need around
700 labels. I tried using your example to test that i could do this but
it didn't seem to work. Here's the code I used, any ideas why it didn't
work? Thanks.

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim lbl As New Label()
Dim i, j As Integer


For i = 1 To 12
For j = 1 To 12
Me.Controls.Add(lbl)
With lbl
' lbl.Top = j + 16
' lbl.Left = i + 16
lbl.BackColor = Color.Gray
lbl.AutoSize = False
lbl.Height = 16
lbl.Width = 16
End With
Next
Next
End Sub
End Class

- David
 
Hi David,

In your code you are actually adding the same label multiple times.
You probably want to add a new label each time, so inside your loop is
where you should have your new label declaration.

Steven
 
Steven said:
Hi David,

In your code you are actually adding the same label multiple times.
You probably want to add a new label each time, so inside your loop is
where you should have your new label declaration.

Steven

Thanks.
 
Back
Top