天天看點

VB.NET 控件數組相關

本例以文本框為類加以說明     '調用方法     Private Sub form_txtArray()         Dim iLoop As Integer         For iLoop = 0 To 5

            Dim txtBox As New Windows.Forms.TextBox

            txtBox.Location = New System.Drawing.Point(iLoop * 20 + 5, 20)

            txtBox.Top = 0

            txtBox.Size = New System.Drawing.Size(20, 10)

            txtArray.addItem(txtBox)

        Next

    End Sub   ======================================================== '取值方法     TextBox1.Text = txtArray.item(2).Text()   ========================================================= '類定義 Imports System

Public Class TextBoxArray

    Inherits Collections.CollectionBase

    Private ReadOnly parentForm As Windows.Forms.Form

    Public Sub New(ByVal pForm As Windows.Forms.Form)

        parentForm = pForm

    End Sub

    Default Public ReadOnly Property item(ByVal index As Integer) As Windows.Forms.TextBox

        Get

            Return Me.List.Item(index)

        End Get

    End Property

    Public Sub addItem()

        Dim txtItem As New Windows.Forms.TextBox

        Me.List.Add(txtItem)

        parentForm.Controls.Add(txtItem)

        txtItem.Text = ""

    End Sub

    Public Sub addItem(ByVal txtItem As Windows.Forms.TextBox)

        Me.List.Add(txtItem)

        parentForm.Controls.Add(txtItem)

    End Sub

    Public Sub RemoveItem()

        If Me.Count > 0 Then

            parentForm.Controls.Remove(Me(Me.Count - 1))

            Me.List.RemoveAt(Me.Count - 1)

        End If

    End Sub End Class

繼續閱讀