Add controls at runtime (To another form)

J

jcrouse

I want to add controls (labels) at runtime to a different form. Here is the
code that is being is under a button on form2:

Dim frm1 As New Form1
Dim MyLabel As New Label
MyLabel.BackColor = BackColor.Black
MyLabel.ForeColor = ForeColor.White
MyLabel.Text = intTopCounter.ToString
MyLabel.TextAlign = ContentAlignment.MiddleCenter
MyLabel.Top = intTopCounter
MyLabel.ContextMenu = frm1.cmnuLabels
MyLabel.Left = 40
MyLabel.Visible = True
intTopCounter = intTopCounter + 30
frm1.Controls.Add(MyLabel)

However, the labels never show up. What am I missing here.

Thanks,
John
 
C

Cor Ligthert

John,

You are almost impossible to help.
You put a message in this newsgroup and than start making another one and
people have no time even to answer you and you even do not look to those
answers you get than.

That was the reason I stopped with it, and I think I do this again.

In this case are you creating a new object instance of form1 what never is
showed.

Cor
 
H

Herfried K. Wagner [MVP]

* "jcrouse said:
I want to add controls (labels) at runtime to a different form. Here is the
code that is being is under a button on form2:

Dim frm1 As New Form1

You are creatung a new instance of 'frm1' instead of using the existing
one. To solve your problem, you will need a reference that points to
the existing instance of the form.
 
I

Imran Koradia

This might seem like a stupid question to ask but are you calling
frm1.Show() after you've added the controls?

Imran.
 
J

jcrouse

Cor....Sorry if I offended you somehow or broke the forum rules. I thought
these were two seperate issues. One to add a control to a different form and
one to delete a specific control. Sorry man. As far as reposting things you
suggested before, you need to realize that your an extremely advanced user
and some of the suggestions you post are just way above my ability to
understand.

Later,
John
 
J

jcrouse

Herfried,
Yeah, I kinda figured that out. I had that all straightened out in my
good app but I created a new app from scratch for testing and didn't have
that part in there. I got it now. Thank you for pointing out my error.

John
 

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

Top