VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "动态按钮"
   ClientHeight    =   3090
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3090
   ScaleWidth      =   4680
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command1 
      Caption         =   "删除按钮"
      Height          =   375
      Index           =   1
      Left            =   240
      TabIndex        =   1
      Top             =   600
      Width           =   975
   End
   Begin VB.CommandButton Command1 
      Caption         =   "添加按钮"
      Height          =   375
      Index           =   0
      Left            =   240
      TabIndex        =   0
      Top             =   120
      Width           =   975
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click(Index As Integer)
    Select Case Index
        Case 0
            n = Command1.UBound + 1                     '通过控件索引上界建立新控件索引序号
            Load Command1(n)                            '加载新的按钮控件
            Command1(n).Top = Command1(n - 1).Top + 500 '设置新添加控件的位置
            Command1(n).Left = Command1(0).Left + 1500
            Command1(n).Visible = True                  '使新添加控件可见
            Command1(n).Caption = "按钮" + Str(n)       '设置新添加控件的标题文字
        Case 1
            If Command1.UBound > 1 Then                 '判断控件是否是动态添加的
                Unload Command1(Command1.UBound)        '删除序号最大的控件
            End If
    End Select
End Sub
