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?
 
Alfredo Luis Torres SerranoConnect With a Mentor Development Director / DBACommented:
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
 
Alfredo Luis Torres SerranoDevelopment Director / DBACommented:
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 SerranoDevelopment Director / DBACommented:
You have to add the control and then modify his properties.l
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Shahan AyyubSenior Software Engineer - iOSCommented:
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 SerranoDevelopment Director / DBACommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.