我想创建一个文本框实例,代码如下,
可是运行的时候却提示“用户定义类型未定义”
请大侠指点迷津,谢谢
Private Sub Command1_Click()
    Dim objTxt As Object
    Set objTxt = New Text1
End Sub

解决方案 »

  1.   

    实在高!总有自己的语法吧!
    Option Explicit
    Private WithEvents btnObj As TextBoxPrivate Sub btnObj_Click()
       MsgBox "This is a dynamically added button."
    End SubPrivate Sub Form_Load()
       Set btnObj = Controls.Add("VB.TextBox", "btnObj")
       With btnObj
          .Visible = True
          .Width = 2000
          .Top = 1000
          .Left = 1000
       End With
    End Sub
      

  2.   

    Dim WithEvents cmd2 As CommandButton    '只支持单个按钮,不支持数组按钮Private Sub cmd1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    Select Case Index
    Case 0
       MsgBox "这是手工创建的按钮!"    '这个有效
    Case Else
       MsgBox "这是程序创建的按钮!" & Index    '但这些就没用了
    End SelectEnd SubPrivate Sub Command1_Click()
     Dim cmd1(11) As CommandButton            '这个没办法创建成 Dim WithEvents  cmd1(11) as CommandButton
     For i = 1 To 11 Set cmd1(i) = Controls.Add("VB.CommandButton", "CmdNew" & i, Form1)
     cmd1(i).Left = 1000 * i
     cmd1(i).Visible = True
     Next i
    End SubPrivate Sub Command2_Click()
     Set cmd2 = Controls.Add("VB.CommandButton", "cmd3")
     cmd2.Left = 1000
     cmd2.Visible = True
    End Sub
    Private Sub cmd2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
       MsgBox "这是程序创建的按钮!"    '这个事件有用
    End Sub
    这是基本用法
      

  3.   

    Option Explicit
    Private WithEvents txtNew As TextBoxPrivate Sub Form_Load()
       Set txtNew = Controls.Add("VB.TextBox", "txtNew")
       With txtNew
          .Visible = True
          .Width = 2000
          .Top = 1000
          .Left = 1000
          .Text = "New TextBox."
       End With
    End Sub
      

  4.   

    在窗体上放一个Text1(0)控件,如果不想见可先隐藏,然后用Load Text1(i) (1=<i<=n)