一首先创建一个Button类型控件数组 .创建Windows应用程序类型的工程添加名为ButtonArray的类并使该类继承 SystemCollectionCollectionBase 类SystemCollectionsCollectionBase类是NET框架类库中为集合操作提供抽象的基类通过对它的继承可以为我们的ButtonArray类具备集合增加删除索引的功能
.为ButtonArray类添加ParentForm属性即控件组所在窗体创建初始化函数(构造函数)
.为控件数组类增加AddItem方法该方法在控件数组类中添加成员
.为控件数组类增加RemoveItem方法该方法在控件数组中删除一个成员
示例代码
Public Class ButtonArray
Inherits SystemCollectionsCollectionBase
Private ReadOnly ParentForm As SystemWindowsFormsForm
Public Sub New(ByVal pForm As SystemWindowsFormsForm)
ParentForm = pForm
End Sub
Default Public ReadOnly Property Item(ByVal index As Integer) As SystemWindowsFormsButton
Get
Return MeListItem(index) ButtonArray的List 属性从CollectionBase 继承
End Get
End Property
Public Sub AddItem()
Dim btnItem As New SystemWindowsFormsButton
MeListAdd(btnItem)
ParentFormControlsAdd(btnItem) 向窗体中增加控件
btnItemTag = MeCount Count属性从CollectionBase 继承
btnItemTop = MeCount *
btnItemLeft =
btnItemText = Button & MeCountToString
AddHandler btnItemClick AddressOf btnItem_Click 绑定事件处理程序
End Sub
Public Sub AddItem(ByVal btnItem As SystemWindowsFormsButton)
MeListAdd(btnItem)
AddHandler btnItemClick AddressOf btnItem_Click 绑定事件处理程序
End Sub
Public Sub RemoveItem()
If MeCount > Then
ParentFormControlsRemove(Me(MeCount ))
MeListRemoveAt(MeCount )
End If
End Sub
Public Sub btnItem_Click(ByVal sender As Object ByVal e As SystemEventArgs)
在这里编写控件数组对点击事件的响应
例如
MsgBox(点击 & senderGetType()ToString & CType(CType(sender Button)Tag String))
End Sub
End Class
二使用创建的控件数组
在Form中放置两个按钮ButtonButton分别测试控件数组的增添删除
双击Form添加代码
Public Class Form
Inherits SystemWindowsFormsForm
Windows窗体设计器生成的代码
Dim Buttons As New ButtonArray(Me)
Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
ButtonsAddItem()
End Sub
Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
ButtonsRemoveItem()
End Sub
End Class
其他的控件数组也可以用类似的方式来实现
例如 Label控件数组
LabelArrayvb代码如下
Public Class LabelArray
Inherits SystemCollectionsCollectionBase
Private ReadOnly ParentForm As SystemWindowsFormsForm
Public Sub New(ByVal pForm As SystemWindowsFormsForm)
ParentForm = pForm
End Sub
Default Public ReadOnly Property Item(ByVal index As Integer) As SystemWindowsFormsLabel
Get
Return MeListItem(index) ButtonArray的List 属性从CollectionBase 继承
End Get
End Property
Public Sub AddItem(ByVal btnItem As SystemWindowsFormsLabel)
MeListAdd(btnItem)
AddHandler btnItemClick AddressOf btnItem_Click 绑定事件处理程序
End Sub
Public Sub btnItem_Click(ByVal sender As Object ByVal e As SystemEventArgs)
在这里编写控件数组对点击事件的响应
例如
MsgBox(点击 & senderGetType()ToString & CType(CType(sender Label)Tag String))
End Sub
End Class
三使用创建的Label控件
在Form中放置两个按钮LabelLabel
双击Form添加代码
Public Class Form
Inherits SystemWindowsFormsForm
#Region Windows 窗体设计器生成的代码
Public Sub New()
MyBaseNew()
该调用是 Windows 窗体设计器所必需的
InitializeComponent()
在 InitializeComponent() 调用之后添加任何初始化
用来绑定label
BindArray()
End Sub
Windows窗体设计器生成的其他代码
#End Region
Dim Labels As New LabelArray(Me)
Public Sub BindArray()
MeLabelTag =
MeLabelTag =
LabelsAddItem(MeLabel)
LabelsAddItem(MeLabel)
End Sub
End Class
然后可以测试点击两个label可以显示相应的Tag的信息