在VB中如何利用程序生成按钮???谢谢!

解决方案 »

  1.   

    Option ExplicitPrivate WithEvents NewButton As CommandButton
    Private Sub Command1_Click()
        If NewButton Is Nothing Then
            Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Frame1)
            'NewButton.Move Command1.Left Command1.Width + 240,Command1.Top
            NewButton.Move 20, 20
            NewButton.Caption = "New Button"
            NewButton.Visible = True
        End If
    End Sub
    Private Sub Command2_Click()
    Form1.Controls.Remove NewButton
    End SubPrivate Sub NewButton_Click()
        MsgBox "New button clicked"
    End Sub
      

  2.   

    通过Add方法实现
    Add方法在Controls集合中添加一个控件并返回一个对控件的引用。Add方法的语法为:Object.Add(ProgID,Name,Container)其中Object为要添加元素的集合,ProgID为标示的字符串.可通过对象浏览器来确定,例如,CommandButton控件的ProgID是VB.CommandButton. Name是控件的名称. Container是包含添加控件的容器,可以为form或Frame控件等等。        Option Explicit    '通过使用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 NewButton_Click()        MsgBox "您选中的是动态增加的按钮!"End Sub
      

  3.   

    Form1.Controls.Add "VB.CommandButton", "cmdObj1"
       With Form1!cmdObj1
          .Visible = True
          .Width = 2000
          .Caption = "Dynamic Button"
       End With
      

  4.   

    Dim a As Control
    Set a = Me.Controls.Add("VB.CommandButton", "cmdObj1")
    a.caption="new commandbutton"
    a.Visible = True
      

  5.   

    先创建一个button数组,只留index=0的那个,其他的都删了。添加类似如下代码:
    Option Explicit
    Dim i As IntegerPrivate Sub Command1_Click(Index As Integer)
    i = i + 1
    Load Command1(i)
    With Command1(i)
        .Visible = True
        .Left = Command1(i - 1).Left + Command1(i - 1).Width + 10
        .Top = Command1(i - 1).Top
    End With
    End SubPrivate Sub Form_Load()
    i = 0
    End Sub运行时点击按钮就会生成新的button
      

  6.   

    楼主相要的应该是这个
    http://community.csdn.net/Expert/topic/3353/3353756.xml?temp=.6354792