Adding controls to form

Ive been trying to add controls to a form, but my code seems to only add the first control and then it doesnt add any more.

Ive tried to make my code simple to try and get it working, but I cant get it working:-
        For x = 1 To 10 Step 1
            Dim lblTest As New Label
            lblTest.Name = "lblLabel" & x
            lblTest.Text = "Label " & x
            lblTest.Top = x * 10
            lblTest.Left = 10
            AddHandler lblTest.DragEnter, AddressOf lblDragEnter
            AddHandler lblTest.MouseDown, AddressOf lblMouseDown
            Me.Controls.Add(lblTest)
        Next

Open in new window


My codes seems to compile ok, but it wont add the controls.

Any ideas?
tonelm54Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfredo Luis Torres SerranoASP .Net DeveloperCommented:
For x = 0 To 9 Step 1
       AddNewLabel(x)
       Me.Controls.Add(newControls(x))
Next

    Public Function AddNewLabel(p1 As Integer) _
    As System.Windows.Forms.Label
        ' Create a new instance of the Label class.
        Dim aLabel As New System.Windows.Forms.Label
        Me.Controls.Add(aLabel)
        aLabel.Size = New Size(60, 13)
        aLabel.Location = New Point(10, p1 * 15)
        aLabel.Text = "lblLabel" & p1
        AddHandler aLabel.DragEnter, AddressOf lblDragEnter
        AddHandler aLabel.MouseDown, AddressOf lblMouseDown
        Return aLabel
    End Function

0
Alfredo Luis Torres SerranoASP .Net DeveloperCommented:
For x = 0 To 9 Step 1
       AddNewLabel(x)
Next

    Public Function AddNewLabel(p1 As Integer) _
    As System.Windows.Forms.Label
        ' Create a new instance of the Label class.
        Dim aLabel As New System.Windows.Forms.Label
        Me.Controls.Add(aLabel)
        aLabel.Size = New Size(60, 13)
        aLabel.Location = New Point(10, p1 * 15)
        aLabel.Text = "lblLabel" & p1
        AddHandler aLabel.DragEnter, AddressOf lblDragEnter
        AddHandler aLabel.MouseDown, AddressOf lblMouseDown
        Return aLabel
    End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alfredo Luis Torres SerranoASP .Net DeveloperCommented:
You have to add the control and then modify his properties.l
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Shahan AyyubSenior Software EngineerCommented:
Just give a quick test, controls are adding properly but they are overlapping to each other.

Increase the offset between controls by changing line 5 to:

   lblTest.Top = x * 30

Open in new window


or change line 5: to

            lblTest.AutoSize = True
            lblTest.Top = x * 10

Open in new window


0
Mez4343Commented:
Your generated Labels are hidden because Top and Left properties are not 'wide' enough. This will display them.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For x = 1 To 10 Step 1
            Dim lblTest As New Label
            lblTest.Name = "lblLabel" & x
            lblTest.Text = "Test " & x
            lblTest.Top = x * 25
            lblTest.Left = x * 20
            AddHandler lblTest.DragEnter, AddressOf lblDragEnter
            AddHandler lblTest.MouseDown, AddressOf lblMouseDown
            Me.Controls.Add(lblTest)
        Next

    End Sub

Open in new window

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For x = 1 To 10 Step 1
            Dim lblTest As New Label
            lblTest.Name = "lblLabel" & x
            lblTest.Text = "Test " & x
            lblTest.Top = x * 25
            lblTest.Left = x * 20
            AddHandler lblTest.DragEnter, AddressOf lblDragEnter
            AddHandler lblTest.MouseDown, AddressOf lblMouseDown
            Me.Controls.Add(lblTest)
        Next

    End Sub

Open in new window

0
Alfredo Luis Torres SerranoASP .Net DeveloperCommented:
Thanks , Glad to help
0
käµfm³d 👽Commented:
You have to add the control and then modify his properties.l
No, you don't.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.