can u add code to objects created on the fly?
can u add code to objects created on the fly?
For example if i run the following code then i want to run some code if a command icon is clicked.
Static X As Integer
X = X + 1
Set Commandbject = Me.Controls.Add("VB.CommandButton", "CommandButton" & X)
With CommandObject
.Caption = "Name"
.Top = 500
.Left = 500
.Height = 500
.Width = 500
.Visible = True
End With
Re: can u add code to objects created on the fly?
Like this, perhaps;
Code:
Dim WithEvents commandobject As CommandButton
Private Sub Command1_Click()
Static X As Integer
X = X + 1
Set commandobject = Me.Controls.Add("VB.CommandButton", "CommandButton" & X)
With commandobject
.Caption = "Name"
.Top = 500
.Left = 500
.Height = 500
.Width = 500
.Visible = True
End With
End Sub
Private Sub commandobject_Click()
Debug.Print "click"
End Sub
Re: can u add code to objects created on the fly?
Quote:
Originally Posted by Doogle
Like this, perhaps;
Code:
Dim WithEvents commandobject As CommandButton
Private Sub Command1_Click()
Static X As Integer
X = X + 1
Set commandobject = Me.Controls.Add("VB.CommandButton", "CommandButton" & X)
With commandobject
.Caption = "Name"
.Top = 500
.Left = 500
.Height = 500
.Width = 500
.Visible = True
End With
End Sub
Private Sub commandobject_Click()
Debug.Print "click"
End Sub
the problem with that is it only runs the code on the last command icon created
Re: can u add code to objects created on the fly?
One of the downsides. Try this post which provides a suitable substitute:
http://vbforums.com/showthread.php?t=372811
Re: can u add code to objects created on the fly?
I always find it easier to do it this way. Add a Command Button to your form in the IDE, set its Index property to 0 and depending on your needs, set its Visible property to False. Then
Code:
Static X As Integer
X = X + 1
If X = 1 Then
Command1(0).Visible = True
Exit Sub
End If
Load Command1(X - 1)
With Command1(X - 1)
.Caption = "Name"
.Top = 500
.Left = Command1(X - 2).Left + 500 ' 500
.Height = 500
.Width = 500
.Visible = True
End With