|
-
Jul 12th, 2000, 06:58 AM
#1
Thread Starter
Hyperactive Member
Hi there,
I know how to create a control at run-time (see code below). The problem is that clicking command1 is supposed to change the caption of the label, at random, but when I click it more than once I get an error saying, 'lbl1 already exists'.
I tried the If statement below (in comments) but it didn't work. I also tried form1.refresh, but that didn't do anything either.
Any help would be appreciated.
Thanks!
Private Sub Command1_Click()
Randomize
'If lbl1.Visible = True Then GoTo Line2
Set lbl1 = Controls.Add("VB.label", "lbl1")
Line2:
With lbl1
.Left = 240
.Top = 100
.Width = 1000
.Height = 500
.Caption = Int(Rnd * 9)
.Visible = True
End With
'End If
End Sub
-
Jul 12th, 2000, 08:01 AM
#2
_______
<?>
this line is adding the control that alread exists
comment it out and see what happens
Set lbl1 = Controls.Add("VB.label", "lbl1")
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Jul 12th, 2000, 08:31 AM
#3
To add Controls.
Code:
Controls.Add "VB.CommandButton", "Command1"
Me!Command1.Move 0, 0
Me!Command1.Caption = "Hello"
Me!Command1.Visible = True
-
Jul 12th, 2000, 09:30 AM
#4
Thread Starter
Hyperactive Member
Thanks,
HesaidJoe,
I commented it out but got an error saying, 'Object required'. Then I put in an error handler, like this:
Private Sub Command1_Click()
Randomize
On Error GoTo errorhandler
Set lbl1 = Controls.Add("VB.label", "lbl1")
With lbl1
.Left = 240
.Top = 100
.Width = 1000
.Height = 500
.Caption = Int(Rnd * 9)
.Visible = True
End With
End If
errorhandler:
With lbl1
.Caption = Int(Rnd * 9)
End With
End Sub
....but I still got a message saying, 'Object required'.
Megatron, thanks for your help but I already know how to create a control at run time. I think I have to use my method, rather than yours, because yours in fine if you want to create a control on a form - whereas in fact I have to create my label in a picture box already on the form.
Everytime command1 is clicked, the caption in the label will change. The problem is that I don't know how to change the label caption without creating the label all over again, and I get an error saying - 'sorry, label1 already exists'.
Any ideas?
Thanks.
-
Jul 12th, 2000, 09:48 AM
#5
_______
<?>
Set lbl1 = Controls.Add("VB.label", "lbl1")
I'm hung on this line..give me the code
that created the label 1st time.
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Jul 12th, 2000, 10:09 AM
#6
_______
<?>..this does it's thing...
Code:
Option Explicit
Private WithEvents lbl1 As Label
Private Sub Form_Load()
Set lbl1 = Controls.Add("VB.label", "lbl1")
With lbl1
.Left = 240
.Top = 100
.Width = 244
.Height = 80
.BackColor = vbWhite
.Visible = True
End With
End Sub
Private Sub Command1_Click()
Randomize
If lbl1.Visible = True Then
With lbl1
.Left = 240
.Top = 100
.Width = 1000
.Height = 500
.Caption = Int(Rnd * 9)
.Visible = True
End With
Else
Set lbl1 = Controls.Add("VB.label", "lbl1")
End If
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Jul 12th, 2000, 10:25 AM
#7
Megatron, thanks for your help but I already know how to create a control at run time. I think I have to use my
method, rather than yours, because yours in fine if you want to create a control on a form - whereas in fact I have to
create my label in a picture box already on the form.
Set the Container property to the name of your PictureBox, then the Control will be inside of it.
-
Jul 12th, 2000, 10:51 AM
#8
_______
<?>..this checks for it then creates it if not there
'beat this thing up a little more
'now I think it is more of what you wanted..
Code:
Option Explicit
Private WithEvents lbl1 As Label
Public sExists As Boolean
'
Public Sub CheckMe()
'
'check to see it the form has a lbl1 on it.
'if it is there make it visible and if
'it's not there create it
'mycontrol = control and increment is increment for forms
'some situtations use more than one form.
'
Dim ctlMyControl As Control
Dim intIncrement As Integer
sExists = True
'
For intIncrement = 0 To Forms.Count - 1
For Each ctlMyControl In Forms(intIncrement).Controls
'
If TypeOf ctlMyControl Is Label Then
'
If ctlMyControl.Name = lbl1 Then
sExists = False
Exit Sub
End If
End If
'
Next ctlMyControl
'
Next intIncrement
End Sub
Public Sub MakeLabel()
'if not label make one
Set lbl1 = Controls.Add("VB.label", "lbl1")
With lbl1
.Left = 240
.Top = 100
.Width = 244
.Height = 80
.BackColor = vbWhite
.Visible = True
End With
'
End Sub
Private Sub Command1_Click()
Randomize
If sExists = False Then Call MakeLabel
Call CheckMe
If lbl1.Visible = True Then
With lbl1
.Left = 240
.Top = 100
.Width = 1000
.Height = 500
.Caption = Int(Rnd * 9)
.Visible = True
End With
End If
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
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
|