VB 高手请进~~

来源:百度知道 编辑:UC知道 时间:2024/09/23 19:22:36
我现在要动态生成一个form,在这个form上要动态生成一个按钮cmdone,要求在双击这个cmdone后,弹出对话框,显示"动态生成的按钮",请问我应该如何实现?

Option Explicit

Dim WithEvents oControl As CommandButton
Dim Newform As Form

Private Sub Command1_Click()
If Not Newform Is Nothing Then MsgBox "Newform 已存在.退出": Exit Sub
Set Newform = New Form1
'-------------------初始化 Newform------------
With Newform
.Caption = "动态生成的窗口"
.Height = 3000
.Width = 5000
End With

'---------------------------------------------------

Newform.Show
'------------------创建控件----------------
Set oControl = Newform.Controls.Add("VB.CommandButton", "Cmdone")
oControl.Caption = "Cmdone"
oControl.Move Newform.Width / 2 - oControl.Width / 2, Newform.Height / 2 - oControl.Height / 2, 1000, 400

oControl.Visible = True

End Sub

Private Sub Form_Unload(Cancel As Integer)
Set oControl = Nothing
End Sub

Priv