|
-
May 30th, 2005, 01:58 PM
#1
Thread Starter
Frenzied Member
Dynamic Control Creation
Hi all, i am trying to add a simple label to my form at run-time, i am experimenting with some code and i am wondering whether this is correct as it is not working...
(VB6)
VB Code:
Private Sub Command1_Click()
Me.Controls.Add "VB.Label", "Label1"
Me!Label1.Caption = "New Label Created At runtime"
Me!Label1.Visible = True
Me!Label1.Left = 100
Me!Label1.Top = 100
Me!Label1.Height = 25
Me!Label1.Width = 150
End Sub
Where am i going wrong ?
-
May 30th, 2005, 02:15 PM
#2
Re: Dynamic Control Creation
The first issue was the use of ! instead of .
The next issue is that the control doesnt exist when the sub starts, so Me.Label1 would be invalid anyway. To get around this you need to use the controls collection, like this:
Me.Controls("Label1").Caption = "New Label Created At runtime"
In this situation I would recomnend using a With block to cut down the code, eg:
VB Code:
Me.Controls.Add "VB.Label", "Label1"
With Me.Controls("Label1")
.Caption = "New Label Created At runtime"
.Visible = True
.Left = 100
.Top = 100
.Height = 25
.Width = 150
End With
An alternative method to create new controls at runtime is to create one at design time, and set its Index property to 0 (so it is part of a control array), and it's Visible property to false (so it wont be shown).
Then at run time you can run code like this (if it is called myLabel):
VB Code:
Load myLabel(1)
myLabel(1).Caption = "Hello"
myLabel(1).Visible = True
This method has the advantage that you can write code for the events at design time (for the "hidden" one, using the Index parameter), without having to know how many new controls will be added.
-
May 30th, 2005, 02:16 PM
#3
Re: Dynamic Control Creation
 Originally Posted by thegreatone
Hi all, i am trying to add a simple label to my form at run-time, i am experimenting with some code and i am wondering whether this is correct as it is not working...
(VB6)
VB Code:
Private Sub Command1_Click()
Me.Controls.Add "VB.Label", "Label1"
Me!Label1.Caption = "New Label Created At runtime"
Me!Label1.Visible = True
Me!Label1.Left = 100
Me!Label1.Top = 100
Me!Label1.Height = 25
Me!Label1.Width = 150
End Sub
Where am i going wrong ?
It seems to be a problem with this bit:
VB Code:
Me!Label1.Height = 25
Me!Label1.Width = 150
When I changed that too:
VB Code:
Me.Controls.Add "VB.Label", "Label1"
Me!Label1.Caption = "New Label Created At runtime"
Me!Label1.Visible = True
Me!Label1.Left = 100
Me!Label1.Top = 100
Me!Label1.Height = Form1.Height
Me!Label1.Width = Form1.Width
Cheers,
RyanJ
-
May 30th, 2005, 02:18 PM
#4
Re: Dynamic Control Creation
thegreatone,
Take a look at Dynamic Control Loading
-
May 30th, 2005, 02:26 PM
#5
-
May 30th, 2005, 02:36 PM
#6
Re: Dynamic Control Creation
You still may not be able to see your control because the height and width are so small.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|