Public Class ButtonArray Inherits System.Collections.CollectionBase Private ReadOnly HostForm As System.Windows.Forms.Form Public Function AddNewButton() As System.Windows.Forms.Button ' Create a new instance of the Button class. Dim aButton As New System.Windows.Forms.Button() ' Add the button to the collection's internal list. Me.List.Add(aButton) ' Add the button to the controls collection of the form ' referenced by the HostForm field. HostForm.Controls.Add(aButton) ' Set intial properties for the button object. aButton.Top = Count * 20 aButton.Left = 100 aButton.Tag = Me.Count aButton.Text = "Button " & Me.Count.ToString Return aButton End Function Public Sub New(ByVal host As System.Windows.Forms.Form) HostForm = host Me.AddNewButton() End Sub Default Public ReadOnly Property Item(ByVal Index As Integer) As _ System.Windows.Forms.Button Get Return CType(Me.List.Item(Index), System.Windows.Forms.Button) End Get End Property Public Sub Remove() ' Check to be sure there is a button to remove. If Me.Count > 0 Then ' Remove the last button added to the array from the host form ' controls collection. Note the use of the default property in ' accessing the array. 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("AddHandler aButton.Click, AddressOf ClickHandler " & CType(CType(sender, _ System.Windows.Forms.Button).Tag, String)) 'AddHandler aButton.Click, AddressOf ClickHandle End Sub End Class Public Class CFSSRV Dim MyControlArray As ButtonArray Private ReadOnly HostForm As System.Windows.Forms.Form Private Sub CFSSRV_QueryAccessibilityHelp(ByVal sender As Object, ByVal e As System.Windows.Forms.QueryAccessibilityHelpEventArgs) Handles Me.QueryAccessibilityHelp Me.Location = New Point(0, 135) Me.Size = New Size(602, 533) End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click ' Call the AddNewButton method of MyControlArray. ' MyControlArray.AddNewButton() MyControlArray.AddNewButton.Text = TRUCK_NO.Text ' Change the BackColor property of the Button 0. MyControlArray(0).BackColor = System.Drawing.Color.Red End Sub Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click ' Visual Basic ' Call the Remove method of MyControlArray. MyControlArray.Remove() MyControlArray(0).BackColor = System.Drawing.Color.Red End Sub Public Sub New() ' This call is required by the designer. InitializeComponent() MyControlArray = New ButtonArray(Me) ' Add any initialization after the InitializeComponent() call. End Sub End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)