VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • VB.NET在窗体上添加任意动态控件

Public Class textboxarray
    Inherits System.Collections.CollectionBase
    Private ReadOnly HostForm As System.Windows.Forms.Form
    Public Function Addtextbox() As System.Windows.Forms.TextBox
        '为textbox类建立新的实例
        Dim atextbox As New System.Windows.Forms.TextBox
        ' 将该按钮添加到集合的内部列表
        Me.List.Add(atextbox)
        '把控件集合中的按钮添加到被HostForm字段引用的窗体
        HostForm.Controls.Add(atextbox)
        ' 设置该按钮对象的初始属性
        atextbox.Top = Count * 25
        atextbox.Left = 150
        atextbox.Tag = Me.Count
        atextbox.Text = ""  'Me.Count.ToString
        AddHandler atextbox.Click, AddressOf ClickHandler
        Return atextbox
    End Function
    Public Sub New(ByVal host As System.Windows.Forms.Form)
        HostForm = host
        Me.Addtextbox()
    End Sub
    Default Public ReadOnly Property Item(ByVal Index As Integer) As _
      System.Windows.Forms.TextBox
        Get
            Return CType(Me.List.Item(Index), System.Windows.Forms.TextBox)
        End Get
    End Property
    Public Sub Remove()
        ' 检查以确保有按钮可以删除
        If Me.Count >= 1 Then
            ' 从主窗体上的控件集合的数组按钮数组中删除最后一个
            ' 注意在访问数组时使用了默认属性
            HostForm.Controls.Remove(Me(Me.Count - 1))
            Me.List.RemoveAt(Me.Count - 1)
        End If
    End Sub
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As _
   System.EventArgs)
        'MessageBox.Show("you have clicked textbox " & CType(CType(sender, _
        'System.Windows.Forms.textbox).Tag, String))
    End Sub
End Class 

相关教程