怎样动态添加用户控件,而且可以添加多个相同的用户控件

解决方案 »

  1.   

    controls.add不就是了
    要不就createwindowex,麻烦点。
      

  2.   

    '以下代码是在程序运行是添加一个按钮:
        '通过使用WithEvents关键字声明一个对象变量为新的命令按钮
        Private WithEvents NewButton As CommandButton
        '增加控件
        Private Sub Command1_Click()
        If NewButton Is Nothing Then
        '增加新的按钮cmdNew
        Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Me)
        '确定新增按钮cmdNew的位置
        NewButton.Move Command1.Left + Command1.Width + 240, Command1.Top
        NewButton.Caption = "新增按钮"
        NewButton.Visible = True
        End If
        End Sub
        '删除控件(注:只能删除动态增加的控件)
        Private Sub Command2_Click()
        If NewButton Is Nothing Then
        Else
        Controls.Remove NewButton
        Set NewButton = Nothing
        End If
        End Sub
        '新增控件的单击事件
        Private Sub NewButton_Click()
         MsgBox "您选中的是动态增加的按钮!"
        End Sub