Option Explicit Private WithEvents btnObj As CommandButtonPrivate Sub btnObj_Click() MsgBox "This is a dynamically added button." End SubPrivate Sub Form_Load() Set btnObj = Controls.Add("VB.CommandButton", "btnObj") With btnObj .Visible = True .Width = 2000 .Caption = "Hello" .Top = 1000 .Left = 1000 End With End Sub
Dim WithEvents Command1 As CommandButtonPrivate Sub Form_Load() Set Command1 = Controls.Add("vb.commandbutton", "command1", Form1) With Command1 .Width = 1800 .Height = 450.Left = (Me.ScaleWidth - .Width) / 2'这两行使动态添加的commandbutton出现在form中心位置 .Top = (Form1.ScaleHeight - .Height) / 2.Caption = "我是打哪来的^o^" .Visible = True End With End SubPrivate Sub Command1_Click() MsgBox "乱按什么啊^&^!!!!" & Chr(10) & Chr(10) & "不理你了!!!", 48 Me.Controls.Remove Command1 End Sub
加个CommandButton用得着这个麻烦吗? 在窗体上放个Command1, 让它的Index=0 (就是CommandButton数组)Private Sub Form_Load() Load Command1(Command1.Ubound+1) Command1(Command1.Ubound).Move MyX, MyY '(MyX, MyY 是你自己的坐标数据) Command1(Command1.Ubound).Caption = MyCaption '(MyCaption是你自己的题头) Command1(Command1.Ubound).Visible=True '让它可见 Command1(Command1.Ubound).ZOrder 0 '让它置顶 End Sub
Private WithEvents btnObj As CommandButtonPrivate Sub btnObj_Click()
MsgBox "This is a dynamically added button."
End SubPrivate Sub Form_Load()
Set btnObj = Controls.Add("VB.CommandButton", "btnObj")
With btnObj
.Visible = True
.Width = 2000
.Caption = "Hello"
.Top = 1000
.Left = 1000
End With
End Sub
Set Command1 = Controls.Add("vb.commandbutton", "command1", Form1)
With Command1
.Width = 1800
.Height = 450.Left = (Me.ScaleWidth - .Width) / 2'这两行使动态添加的commandbutton出现在form中心位置
.Top = (Form1.ScaleHeight - .Height) / 2.Caption = "我是打哪来的^o^"
.Visible = True
End With
End SubPrivate Sub Command1_Click()
MsgBox "乱按什么啊^&^!!!!" & Chr(10) & Chr(10) & "不理你了!!!", 48
Me.Controls.Remove Command1
End Sub
在窗体上放个Command1, 让它的Index=0 (就是CommandButton数组)Private Sub Form_Load()
Load Command1(Command1.Ubound+1)
Command1(Command1.Ubound).Move MyX, MyY '(MyX, MyY 是你自己的坐标数据)
Command1(Command1.Ubound).Caption = MyCaption '(MyCaption是你自己的题头)
Command1(Command1.Ubound).Visible=True '让它可见
Command1(Command1.Ubound).ZOrder 0 '让它置顶
End Sub