熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

在VB.Net中創建使用控件數組

2013-11-13 11:05:32  來源: .NET編程 
首先創建一個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的信息


From:http://tw.wingwit.com/Article/program/net/201311/15141.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.