Create a control array in VB. Net

Source: Internet
Author: User

Create a control array in VB. Net

First, create a Button control array:

1. Create a project of the "Windows application" type, add a class named ButtonArray, and make the class inherit the System. Collection. CollectionBase class. System. collections. the CollectionBase class is. NET Framework Class Library provides abstract base classes for set operations. By inheriting them, we can provide the function of adding, deleting, and indexing a set for our ButtonArray class.

2. Add the ParentForm attribute to the ButtonArray class, that is, the form of the control group, and create the initialization function (constructor );

3. Add the AddItem Method to the control array class. This method adds members to the control array class;

4. Add the RemoveItem Method to the control array class. This method deletes a member from 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) the List attribute of ButtonArray 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 property inherited from CollectionBase

BtnItem. Top = Me. Count * 30

BtnItem. Left = 200

BtnItem. Text = "Button" & Me. Count. ToString

AddHandler btnItem. Click, AddressOf btnItem_Click bind event handler

End Sub


Public Sub AddItem (ByVal btnItem As System. Windows. Forms. Button)

Me. List. Add (btnItem)
AddHandler btnItem. Click, AddressOf btnItem_Click bind event handler

End Sub
Public Sub RemoveItem ()

If Me. Count> 0 Then

ParentForm. Controls. Remove (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)

Write control array response to click event here

For example:

MsgBox ("click:" & sender. GetType (). ToString & CType (sender, Button). Tag, String ))

End Sub

End Class


 

 

Use the created control array

Place two buttons Button1 and Button2 in Form1 to test the addition and deletion of the control array.

Double-click Form to add code:

Public Class Form1

Inherits System. Windows. Forms. Form

 

...... Code generated by the Windows Form Designer ......

Dim Buttons As New ButtonArray (Me)

 

Private Sub button#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 also be implemented in a similar way.
For example, 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) the List attribute of ButtonArray 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 bind event handler

End Sub


Public Sub btnItem_Click (ByVal sender As Object, ByVal e As System. EventArgs)

Write control array response to click event here

For example:

MsgBox ("click:" & sender. GetType (). ToString & CType (sender, Label). Tag, String ))

End Sub
End Class


Use the created Label Control
Place two buttons Label1 and Label2 in Form1

Double-click Form to add code:

Public Class Form1

Inherits System. Windows. Forms. Form


# Region "code generated by Windows Form Designer"

Public Sub New ()
MyBase. New ()

This call is required by the Windows Form Designer.
InitializeComponent ()

Add any initialization after InitializeComponent () is called
Used to bind a label
BindArray ()

End Sub
...... Other code generated by the Windows Form 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

Then you can test and click two labels to display the corresponding Tag information.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.