增加可以使用 load command1(i) 的方法,前提是已经存在index 为0的 command1控件或者 Private WithEvents btnObj As CommandButton Set btnObj = Controls.Add("VB.CommandButton", "btnObj")减少使用 unload command1(i),但是Controls.Add方法加上去的控件和控件箱中 加上去的控件不能这样卸载 Option Explicit Private WithEvents btnObj As CommandButtonPrivate Sub Command3_Click() Unload Command1(1) '可以卸载 Unload btnObj '不能运行 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
Load Command1(1) Command1(1).Top = Command1(0).Top + 500 Command1(1).Visible = True End Sub
Private Sub Form_Load() Dim intTmp As Integer, objTmp As Object For intTmp = 1 To 5 Step 1 Set objTmp = Me.Controls.Add("VB.CommandButton", "cmd" & intTmp, Me) objTmp.Caption = intTmp objTmp.Move (Me.ScaleWidth - 1200) \ 2, intTmp * 350, 1200, 300 objTmp.Visible = True Next intTmp End Sub
Private Sub Command1_Click() Dim intTmp As Integer For intTmp = 1 To 5 Step 1 Me.Controls.Remove "cmd" & intTmp Next intTmp End Sub
command1控件或者
Private WithEvents btnObj As CommandButton
Set btnObj = Controls.Add("VB.CommandButton", "btnObj")减少使用 unload command1(i),但是Controls.Add方法加上去的控件和控件箱中
加上去的控件不能这样卸载
Option Explicit
Private WithEvents btnObj As CommandButtonPrivate Sub Command3_Click()
Unload Command1(1) '可以卸载
Unload btnObj '不能运行
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
Load Command1(1)
Command1(1).Top = Command1(0).Top + 500
Command1(1).Visible = True
End Sub
Private Sub Form_Load()
Dim intTmp As Integer, objTmp As Object For intTmp = 1 To 5 Step 1
Set objTmp = Me.Controls.Add("VB.CommandButton", "cmd" & intTmp, Me)
objTmp.Caption = intTmp
objTmp.Move (Me.ScaleWidth - 1200) \ 2, intTmp * 350, 1200, 300
objTmp.Visible = True
Next intTmp
End Sub
Dim intTmp As Integer For intTmp = 1 To 5 Step 1
Me.Controls.Remove "cmd" & intTmp
Next intTmp
End Sub