
(1)新建一个VB6工程
(2)Form1窗体代码
Option Explicit'定义要动态添加的命令按钮变量(带事件处理)
Dim WithEvents cmdX As CommandButton
'点击动态添加的按钮,d出对话框
Private Sub cmdX_Click()
MsgBox "动态添加的命令按钮!!!"
End Sub
'鼠标双击窗体,动态添加一个命令按钮
Private Sub Form_DblClick()
If cmdX Is Nothing Then
Set cmdX = Controls.Add("VB.CommandButton", "cmd1")
cmdX.Caption = "点击我呀"
cmdX.Width = 4500
cmdX.Move 150, 150
cmdX.Visible = True
End If
End Sub
(3)运行
启动后
鼠标双击窗体
点击命令按钮,d出一个对话框
Private WithEvents NewTextBox As TextBox'通过使用WithEvents关键字声明一个对象变量为新的命令按钮
Private Sub Command1_Click()
If NewTextBox Is Nothing Then
Set NewTextBox = Controls.Add("VB.TextBox", "cmdNew", Form1)
NewTextBox.Move 200, 200
NewTextBox.Width = Form1.Width - 450
NewTextBox.Height = Form1.Height - 1400
NewTextBox.Visible = True
End If
End Sub
Private Sub Command2_Click()
If NewTextBox Is Nothing Then
Exit Sub
Else
Controls.Remove NewTextBox
Set NewTextBox = Nothing
End If
End Sub
经验证没有错误。。Private Sub Form_Load()
Set Timer1 = Controls.Add("vb.timer", "Timer1")
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
Label1.Caption = Timer
End Sub
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)