2个回答
展开全部
Option Explicit
Private WithEvents NewButton As CommandButton
'通过使用WithEvents关键字声明一个对象变量为新的命令按钮
Private Sub Command1_Click()
If NewButton Is Nothing Then
Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Form1)
'增加新的按钮cmdNew
NewButton.Move Command1.Left + Command1.Width + 240, Command1.Top
'确定新增按钮cmdNew的位置
NewButton.Caption = "动态添加的按钮"
NewButton.Visible = True
'显示该按钮
End If
End Sub
Private Sub Command2_Click()
If NewButton Is Nothing Then
Exit Sub
Else
Controls.Remove NewButton
Set NewButton = Nothing
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 9
Set NewButton = Controls.Add("VB.CommandButton", "Cmd" & i, Form1)
'增加新的按钮cmdNew
NewButton.Move 100 + i * 500, 500, 500, 120
'确定新增按钮cmdNew的位置
NewButton.Caption = i
NewButton.Visible = True
Next i
End Sub
Private Sub NewButton_click()
MsgBox "这是动态增加的按钮,你可以单击“删除控件”按钮删除它", vbDefaultButton1, "Click"
End Sub
Private Sub cmd1_click()
End
End Sub
你参考一下,这是创建Command 的
Private WithEvents NewButton As CommandButton
'通过使用WithEvents关键字声明一个对象变量为新的命令按钮
Private Sub Command1_Click()
If NewButton Is Nothing Then
Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Form1)
'增加新的按钮cmdNew
NewButton.Move Command1.Left + Command1.Width + 240, Command1.Top
'确定新增按钮cmdNew的位置
NewButton.Caption = "动态添加的按钮"
NewButton.Visible = True
'显示该按钮
End If
End Sub
Private Sub Command2_Click()
If NewButton Is Nothing Then
Exit Sub
Else
Controls.Remove NewButton
Set NewButton = Nothing
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 9
Set NewButton = Controls.Add("VB.CommandButton", "Cmd" & i, Form1)
'增加新的按钮cmdNew
NewButton.Move 100 + i * 500, 500, 500, 120
'确定新增按钮cmdNew的位置
NewButton.Caption = i
NewButton.Visible = True
Next i
End Sub
Private Sub NewButton_click()
MsgBox "这是动态增加的按钮,你可以单击“删除控件”按钮删除它", vbDefaultButton1, "Click"
End Sub
Private Sub cmd1_click()
End
End Sub
你参考一下,这是创建Command 的
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询