Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make a New . . . 1

Status
Not open for further replies.

Caper

Technical User
Aug 11, 2001
9
US
How do I programaticaly create a new label on my form at run-time if I don't already have one on the form? This seems like one of those simple things that probably isn't so simple...

Thanks for any help or guidance!
 
Yes, this creates a new button based on a command button that's already on Form1, but how about creating a new label from scratch? Or is there a better way to place a text label in a frame next to a point that changes based on user input?

Thanks.
 
<sigh>

Firstly that's not quite what the example code illustrates, and secondly all the information you need is in that thread. You just need to read the first message and the second message, and then modify the code presented in the second message with the info given in the first message, thus create a project with a single form that has a command button and drop in the following (a simple modification of JustinEzequiel's code to work with a label instead of a command button):
[tt]
Option Explicit

Private WithEvents cmdCreated As CommandButton

Private Sub Command1_Click()
Set cmdCreated = Form1.Controls.Add(&quot;VB.CommandButton&quot;, &quot;cmdCreated&quot;)

cmdCreated.Top = 0
cmdCreated.Left = 0
cmdCreated.Caption = &quot;Created&quot;
cmdCreated.Visible = True
End Sub

Private Sub cmdCreated_Click()
MsgBox &quot;cmdCreated event&quot;
End Sub

 
Oops, final Sub should read:

Private Sub lblCreated_Click()
MsgBox &quot;lblCreated event&quot;
End Sub
 
OK, got it to work. Here's the code I used:

Code:
Option Explicit
Private lblLabel1 As Label

Private Sub Command1_Click()
  Set lblLabel1 = Form1.Controls.Add(&quot;VB.label&quot;, &quot;lblLabel1&quot;)
  lblLabel1.Top = 0
  lblLabel1.Left = 0
  lblLabel1.Caption = &quot;this is label1&quot;
  lblLabel1.Visible = True
End Sub

My understanding of what's going on here is that an object variable is being declared in the general section of the form, and upon clicking Command1 a VB.label is created, set to the variable, and given a name. Then some of it's properties start getting tweaked.

I'm (obviously) new to object variables, and they're still a slippery concept for me. Thanks for the help - <sigh> and all!
 
Sorry about the sigh. It was 02:30 in the morning after along day at work...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top