在vb.net中可以是Controls.Add方法向容器添加控件,在vb6中不行,但是可以使用数组的方法添加,例如你要添加一个TextBox可以事先在容器中放一个text1(0)(数组,下标为0)可见为False,要添加可以load text1(1)语句实现,设置它的left,top,visivle属性

解决方案 »

  1.   

    Dim WithEvents ctl As CommandButton    'because the button is dynamically load ,only this way can use the eventForm1.Controls.Add "vb.commandbutton", "cmdok"   'dynamical load the button
        
        With Form1!cmdok
            .Visible = True
            .Width = 500
            .Caption = "hello"
            .Left = Me.Width / 2
            .Top = Me.Height / 2
        End With
        Set ctl = Form1!cmdok
    还有一个方法就是用控件数组了,如楼上兄弟所说的
      

  2.   

    Private Sub Form_Load()
    Command1(0).Left = 2222
    Command1(0).Top = 2222
    Load Command1(1)
    Command1(1).Left = 0
    Command1(1).Top = 0
    Command1(1).Caption = "动态按钮"
    Command1(1).Visible = True
    End Sub
      

  3.   

    litsnake1 谢谢你的代码,由于我是初学,你的代码虽然可以运行但是里面还有些东西我不太明白。比如第一句声明中用了WithEvent关键字,是什么意思,还有Form1.Controls.Add "vb.commandbutton", "cmdok"中cmdok是什么意思,最后一句set ctl1=Form1!cmdok 是什么意思,我也不明白,!这个符号有什么作用?希望你能解释一下,谢谢!
      

  4.   

    WithEvent是定义一个带事件的对象,既然你不属性VB的类,建议你使用控件数组
      

  5.   


    Container 属性
          返回或设置 Form 上控件的容器。在设计时不能使用。语法Set object.Container [= container]Container 属性的语法包含下面部分:部分 描述 
    object 对象表达式,其值是“应用于”列表中的一个对象。 
    container 一个对象表达式其值是能够作为别的控件容器使用的对象,按照说明的描述。 
    说明下面的控件能够容纳别的控件: Frame 控件
    PictureBox 控件.
    SSTab 控件 Container 属性示例
    该例子演示在 Form 对象上把 CommandButton 控件从一个容器移动到另一个容器。要试用此例,先将以下代码粘贴到包含一个 Frame 控件、一个 PictureBox 控件和一个 CommandButton 控件的窗体的声明部分,然后按下 F5 键。Private Sub Form_Click()
       Static intX As Integer
       Select Case intX
          Case 0
             Set Command1.Container = Picture1
             Command1.Top= 0
             Command1.Left= 0
          Case 1
             Set Command1.Container = Frame1
             Command1.Top= 0
             Command1.Left= 0
          Case 2
             Set Command1.Container = Form1
             Command1.Top= 0
             Command1.Left= 0
       End Select
       intX = intX + 1
    End Sub