Create | controls | arrays
Create a Use control array in vb.net
First create a button type control array:
1, create a project of type "Windows Application", add a class named Buttonarray, and make the class inherit the System.Collection.CollectionBase class. The System.Collections.CollectionBase class is a base class in the. NET Framework class library that provides abstractions for collection operations, and by inheriting it we have the ability to add, delete, and index collections for our Buttonarray classes.
2. Add the ParentForm property to the Buttonarray class, that is, the form where the control group is located, and create the initialization function (constructor);
3. Add a AddItem method to the control array class, which adds a member to the control array class;
4. Add the RemoveItem method for the control array class, which deletes a member in the control array.
Sample code:
Public Class Buttonarray
Inherits System.Collections.CollectionBase
Private ReadOnly ParentForm as System.Windows.Forms.Form
Public Sub New (ByVal pform as System.Windows.Forms.Form)
ParentForm = Pform
End Sub
Default Public ReadOnly Property Item (ByVal index as Integer) as System.Windows.Forms.Button
Get
Return Me.List.Item (Index) ' Buttonarray List property inherits from CollectionBase
End Get
End Property
Public Sub AddItem ()
Dim Btnitem as New System.Windows.Forms.Button
ME.LIST.ADD (Btnitem)
PARENTFORM.CONTROLS.ADD (Btnitem) ' Add controls to the form
Btnitem.tag = Me.count ' Count property inherits from CollectionBase
Btnitem.top = Me.count * 30
Btnitem.left = 200
Btnitem.text = "button" & Me.Count.ToString
AddHandler Btnitem.click, AddressOf Btnitem_click ' binding event handler
End Sub
Public Sub AddItem (ByVal Btnitem as System.Windows.Forms.Button)
ME.LIST.ADD (Btnitem)
AddHandler Btnitem.click, AddressOf Btnitem_click ' binding event handler
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
Public Sub Btnitem_click (ByVal sender as Object, ByVal e as System.EventArgs)
' Here, write the response of the control array to the Click event
e.g.
MsgBox ("click:" & Sender.) GetType (). ToString & CType (CType (sender, Button). Tag, String))
End Sub
End Class
Using the created control array
Place two buttons Button1, Button2 in the Form1, and test the addition and deletion of the control array respectively.
Double-click the form to add code:
Public Class Form1
Inherits System.Windows.Forms.Form
...... Code generated by the Windows Forms Designer ...
Dim Buttons as New Buttonarray (Me)
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Buttons.additem ()
End Sub
Private Sub button2_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button2.click
Buttons.removeitem ()
End Sub
End Class
Other control arrays can be implemented in a similar way
For example, a Label control array
The Labelarray.vb code is as follows:
Public Class Labelarray
Inherits System.Collections.CollectionBase
Private ReadOnly ParentForm as System.Windows.Forms.Form
Public Sub New (ByVal pform as System.Windows.Forms.Form)
ParentForm = Pform
End Sub
Default Public ReadOnly Property Item (ByVal index as Integer) as System.Windows.Forms.Label
Get
Return Me.List.Item (Index) ' Buttonarray List property inherits from CollectionBase
End Get
End Property
Public Sub AddItem (ByVal Btnitem as System.Windows.Forms.Label)
ME.LIST.ADD (Btnitem)
AddHandler Btnitem.click, AddressOf Btnitem_click ' binding event handler
End Sub
Public Sub Btnitem_click (ByVal sender as Object, ByVal e as System.EventArgs)
' Here, write the response of the control array to the Click event
e.g.
MsgBox ("click:" & Sender.) GetType (). ToString & CType (CType (sender, Label). Tag, String))
End Sub
End Class
Using the created Label control
Place two buttons in Form1 Label1, Label2
Double-click the form to add code:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region "code generated by the Windows forms Designer"
Public Sub New ()
MyBase.New ()
' This call is required by the Windows Forms Designer.
InitializeComponent ()
' Add any initialization after the InitializeComponent () call
' To bind the label
Bindarray ()
End Sub
...... Additional code generated by the Windows Forms Designer ...
#End Region
Dim Labels as New Labelarray (Me)
Public Sub Bindarray ()
Me.Label1.Tag = "1111"
Me.Label2.Tag = "222"
Labels.additem (ME.LABEL1)
Labels.additem (ME.LABEL2)
End Sub
End Class
You can then test the click of two labels to display the corresponding tag information.