|
|
Title | Create a new button using Controls.Add and respond to its click event |
Description | This example shows how to create a new button using Controls.Add and respond to its click event in Visual Basic 6. |
Keywords | button, Controls.Add, event |
Categories | Controls, Tips and Tricks |
|
|
Declare a variable using the WithEvents keyword for the new button. Set this variable to the new button created with Controls.Add. Declaring the variable WithEvents means it gets events just like a button created at design time.
|
|
Private WithEvents new_button As CommandButton
Private Sub Command1_Click()
Command1.Enabled = False
Set new_button = Me.Controls.Add("VB.CommandButton", _
"cmdNew")
With new_button
.Move Command1.Left, Command1.Top + Command1.Height _
+ 120
.Caption = "New Button"
.Visible = True
End With
End Sub
Private Sub new_button_Click()
MsgBox "Clicked"
End Sub
|
|
|
|
|
|