Design Time Label

  • Thread starter Thread starter pmclinn
  • Start date Start date
P

pmclinn

I'm looking to add a lable control to my windows form when a button is
pressed. The following code does not seem to render the button. How
do I correct this issue?


Dim newLabel As New Label
newLabel.Size = New System.Drawing.Size(272, 16)
newLabel.Name = "NewLabels"
newLabel.BackColor = System.Drawing.Color.Black
newLabel.ForeColor = System.Drawing.Color.Lime
newLabel.Location = New System.Drawing.Point(16, 336)
newLabel.Text = "NewLabels"
newLabel.BringToFront()
newLabel.Visible = True
Me.Controls.Add(newLabel)
 
I used that code with an existing program of mine, and the BringToFront
didn't seem to work. So I moved the BringToFront statement to the end
of your code, and it worked fine. So try to set its z-order after you
add it to your form.

hth
 
Ok, this worked on a one by one basis.

To take it a step further, how would I create a class of this control
and this add it to a form programmatically?
 
If you are wanting to create a variation of the Label to customize it
to your program, you would have to create a class that inherits the
Label Class and override its functions. Then, you could use the same
code but instead use your classname instead of Label.

If you were wanting to create one sub that creates a bunch of labels,
you could just use a 'for' statement and make sure to add the
newLabel.BringToFront at the end of your 'for'. Put it in your own sub
so you can specify the parameters. ex: getAnotherLabel(name, text,
posx, posy, w, h, etc...)

//
for i as integer = 0 to 8
' call the subname where your code is
getAnotherLabel(....)
next
\\

that would create 9 labels

I don't know if this is what you were looking for, if not please
clarify.

hth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top