Question Create many Labels

Rafis

New member
Joined
Jul 28, 2010
Messages
4
Programming Experience
Beginner
I need to add some Labels to an existing Form.
I don't know how many Labels I will use, so I want to create them at run-time.
Any suggestions?
 
Hi,

You can dynamically create any control type that you need "on the fly". Add a button to a new form and add the code below:-

VB.NET:
Public Class Form1
 
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Static Counter As Integer
 
    Counter += 1
    Dim myNewLabel As New Label
    With myNewLabel
      .Name = "Label" & Counter
      .Text = "New Label"
      .Left = 100
      .Top = 20 * Counter
    End With
    Me.Controls.Add(myNewLabel)
  End Sub
End Class
Press the button numerous times to see new labels being added to the form.

Cheers,

Ian
 
Thanks Ian!
Please help me in somthing else:
Some of the Labes that are located on the Form are hidden by other controls (such as PictureBox).
How to bring theme always to the top of the Form? I tryed "BringToFront()", but it doesn't do the job.
 
Hi,

After the control is added to the controls collection you can then call:-

myNewLabel.BringToFront()

This will display the new control on top of any other controls. That said, what you need to be thinking about is where exactly you want to position the labels when you create them. Currently, I have used .Left=100, .Top=20*Counter. Where do you need them to be positioned?

Cheers,

Ian.
 
Last edited:
Thanks Ian!
Please help me in somthing else:
Some of the Labes that are located on the Form are hidden by other controls (such as PictureBox).
How to bring theme always to the top of the Form? I tryed "BringToFront()", but it doesn't do the job.

Did you by any chance call BringToFront before they had been added to the Controls collection? If they're not is such a collection then there's nothing to bring them to the front of.
 
Problem is that the Labels are located on an empty PictureBox.
All I need is to see the labels after creation, but when any Label is located on the PitureBox, it becomes invisible.
and, I used BringToFront() while creating the labels, but it does nothing.
I think I should use "SetTopLevel", but I can't find the right way to use this metod.
 
No you should not use SetTopLevel. You should use BringToFront because that is the method that brings a control to the front of the z-order and that is what you want to do. If it's not working then you're doing it wrong. Perhaps if you were to show us your code then we wouldn't have to guess what's wrong with it.
 
Hi,

You have missed both my own and jmcilhinney's point. You have to add the control to the controls collection first before you can call BringToFront. See here:-

VB.NET:
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Static Counter As Integer
 
    Counter += 1
    Dim myNewLabel As New Label
    With myNewLabel
      .Name = "Label" & Counter
      .Text = "New Label"
      .Left = 50
      .Top = 20 * Counter
    End With
    Me.Controls.Add(myNewLabel)
    myNewLabel.BringToFront()
  End Sub
Cheers,

Ian.
 
Back
Top