Private Sub Form_Click()
  Me.Controls.Add "vb.CommandButton", "Pic1", Form1
          With Form1!pic1
                  .Top = 600
                  .Left = 100
                  .Width = 1500
                  .Height = 800
                  .BackColor = vbRed
                  .Visible = True
          End With
          
 
    Me.Controls.Add "vb.TextBox", "Pic2", Form1
          With Form1!pic2
                  .Top = 100
                  .Left = 100
                  .Width = 1500
                  .Height = 600
                  .BackColor = vbRed
                  .Visible = True
          End With
End Sub如何用控件数组实现呢?

解决方案 »

  1.   

    现在窗口上定义个commandbutton和textbox,index属性设为0,程序运行中刚通过new创建commandbutton和textbox。
      

  2.   

    貌似控件数组的起始元素必须在设计时添加,而不能是运行时new的
      

  3.   

    给你摘一段MSDN里的话:You can add and remove controls in a control array at run time using the Load and Unload statements. However, the control to be added must be an element of an existing control array. You must have created a control at design time with the Index property set, in most cases, to 0.具体可到MSDN里查“Working with Control Arrays”
      

  4.   

    基本同意2楼,但是不是new方法,是load方法加载:
    先在form上定义一个button和textbox,设置index=0
    然后点击事件中写:
    static index
    index=index+1
    load button(index)
    button(index).caption="bt1"
    ...
    load textbox(index)
    textbox(index).text="txt1"
    ...
      

  5.   

    呵呵,看错了。以为1楼是楼主的回复,另外1楼的意思也理解错了,上面的发言先作废。具体可这样做:(1)先在设计视图下,在窗体上定义个commandbutton和textbox,index属性设为0,名字分别为,比如说,cmdTest和txtTest
    (2)然后用如下代码
    Private Sub Form_Click()
        Load txtTest(txtTest.UBound + 1)
        With txtTest(txtTest.UBound)
            .Top = 100
            .Left = 100 + 1500 * txtTest.UBound
            .Width = 1500
            .Height = 600
            .BackColor = vbRed
            .Visible = True
        End With
        
        Load cmdTest(cmdTest.UBound + 1)
        With cmdTest(cmdTest.UBound)
            .Top = 600
            .Left = 100 + 1500 * cmdTest.UBound
            .Width = 1500
            .Height = 600
            .BackColor = vbRed
            .Visible = True
        End With
    End Sub
      

  6.   

    to slowgraceindex属性如何 设为0,是加在代码里吧?
    另外,txtTest.UBound中的UBound是什么?俄
    我在 txtTest里没有看到这个属性啊?
      

  7.   


    index属性如何 设为0,是加在代码里吧? 
    ==在设计视图里,选中txtTest控件,在属性页里找Index这个属性,设置它的值为0
    另外,txtTest.UBound中的UBound是什么?我在 txtTest里没有看到这个属性啊?
    ==ubound是取数组的上界。你正确设置控件的index后,就会出现这个属性。
      

  8.   

    非常 感谢,已经可以了,还有一个问题 
    用下面的方法是 手动定义了两个控件变量
    Dim WithEvents cmdtest As VB.CommandButton '声明一个事件的按钮
    Dim WithEvents txttest As TextBox '声明一个文本框而  在窗体上 放上这两个控件   ,是 系统默认定义了这两个控件变量吧?i  这两种 定义出来的 变量在使用上 有何 区别呢?
     
    地二种  有 index属性,而第一种没有  index属性,怎么 在第一种方法定义出来的  控件变量里使用 index属性呢?
      

  9.   

    在窗体上 放上这两个控件  ,是 系统默认定义了这两个控件变量吧?
    ==是的。系统自动为你的窗体类加了两个成员控件变量。这两种 定义出来的 变量在使用上 有何 区别呢? 
    ==用with events定义的变量,只能用来引用其他已存在的对象变量。地二种  有 index属性,而第一种没有  index属性,怎么 在第一种方法定义出来的  控件变量里使用 index属性呢? 
    ==没有办法,除非你把with events的变量引用到你在窗体上手工建立的控件上