Home All Groups Group Topic Archive Search About

Using code to add controls

Author
13 Oct 2006 12:31 AM
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

Author
13 Oct 2006 12:53 AM
Steven Nagy
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
Author
13 Oct 2006 1:39 AM
David Pick
Steven Nagy wrote:
Show quoteHide quote
> 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
Author
13 Oct 2006 3:00 AM
Steven Nagy
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
Author
13 Oct 2006 3:42 AM
David Pick
Steven Nagy wrote:
> 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.