怎样动态生成一个按钮或者一个列表框???急

解决方案 »

  1.   

    建立Command1(i)数组Load Command1(i)
      

  2.   

    Option ExplicitDim WithEvents btn As VB.CommandButton
    Dim WithEvents lst As VB.ListBoxPrivate Sub Command1_Click()
    Set btn = Controls.Add("VB.CommandButton", "Button1")
        btn.Caption = "New Button"
        btn.Visible = True
    End SubPrivate Sub btn_click()
        MsgBox "New Button Clicked"
    End SubPrivate Sub Command2_Click()
    Set lst = Controls.Add("VB.ListBox", "List1")
        lst.Visible = True
        lst.Left = 1400
        lst.AddItem "This is a new ListBox"
    End SubPrivate Sub lst_click()
        MsgBox lst.List(lst.ListIndex)
    End Sub
      

  3.   

    先放一个控件,index设为0。  运行时用Load 语句添加。例:窗体上有一个Command控件,名字为cmdTest,Index属性为0Private Sub Form_Load()
    Dim i As IntegerFor i = 1 To 5
    Load cmdTest(i)
    cmdTest(i).Top = cmdTest(i - 1).Top + 200
    cmdTest(i).Visible = True
    Next
    End Sub
    这样就可以在窗体上再加5个TextBox出来了。
    卸载用Unload txtTest(Index)即可。
      

  4.   

    以下在form中:
    Option Explicit
    Private WithEvents NewButton As CommandButton
    Private WithEvents NewOption As OptionButton
    Private Sub Command1_Click()
           If NewOption Is Nothing Then
            Set NewOption = _
                Controls.Add("VB.OptionButton", _
                "Option", Me)
            NewOption.Move 3000, 1000, 1000, 300
            NewOption.Caption = "New Option"
            NewOption.Visible = True
        End If
        If NewButton Is Nothing Then
            Set NewButton = _
                Controls.Add("VB.CommandButton", _
                "cmdNew", Me)
            NewButton.Move _
                Command1.Left + Command1.Width + 240, _
                Command1.Top
            NewButton.Caption = "New Button"
            NewButton.Visible = True
        End If
    End Sub
    Private Sub NewButton_Click()
        MsgBox "New button clicked"
    End SubPrivate Sub NewOption_Click()
     MsgBox "New option clicked"
    End Sub
      

  5.   

    有两种方法
    1.Load txt(n)
    Unload txt(n)2.Me.controls.add 
    Control.remove "control Name"两个是匹配使用的
    load 的不能removeadd的,不能 unload
      

  6.   

    想说的,别人都说清楚了。  不过有一个问题:用Add方法怎么生成控件数组?