可以實現(xiàn)
公司主營業(yè)務(wù):成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)公司推出水磨溝免費做網(wǎng)站回饋大家。
首先創(chuàng)建一個Button類型控件數(shù)組:
1、創(chuàng)建“Windows應(yīng)用程序”類型的工程,添加名為ButtonArray的類,并使該類繼承 System.Collection.CollectionBase 類。System.Collections.CollectionBase類是.NET框架類庫中為集合操作提供抽象的基類,通過對它的繼承可以為我們的ButtonArray類具備集合增加、刪除、索引的功能。
2、為ButtonArray類添加ParentForm屬性,即控件組所在窗體,創(chuàng)建初始化函數(shù)(構(gòu)造函數(shù));
3、為控件數(shù)組類增加AddItem方法,該方法在控件數(shù)組類中添加成員;
4、為控件數(shù)組類增加RemoveItem方法,該方法在控件數(shù)組中刪除一個成員。
示例代碼:
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 屬性從CollectionBase 繼承
End Get
End Property
Public Sub AddItem()
Dim btnItem As New System.Windows.Forms.Button
Me.List.Add(btnItem)
ParentForm.Controls.Add(btnItem) '向窗體中增加控件
btnItem.Tag = Me.Count 'Count屬性從CollectionBase 繼承
btnItem.Top = Me.Count * 30
btnItem.Left = 200
btnItem.Text = "Button" Me.Count.ToString
AddHandler btnItem.Click, AddressOf btnItem_Click '綁定事件處理程序
End Sub
Public Sub AddItem(ByVal btnItem As System.Windows.Forms.Button)
Me.List.Add(btnItem)
AddHandler btnItem.Click, AddressOf btnItem_Click '綁定事件處理程序
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)
'在這里編寫控件數(shù)組對點擊事件的響應(yīng)
'例如:
MsgBox("點擊:" sender.GetType().ToString CType(CType(sender, Button).Tag, String))
End Sub
End Class
使用創(chuàng)建的控件數(shù)組
在Form1中放置兩個按鈕Button1、Button2,分別測試控件數(shù)組的增添、刪除。
雙擊Form添加代碼:
Public Class Form1
Inherits System.Windows.Forms.Form
……Windows窗體設(shè)計器生成的代碼……
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
其他的控件數(shù)組也可以用類似的方式來實現(xiàn)
例如 Label控件數(shù)組
LabelArray.vb代碼如下:
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 屬性從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 '綁定事件處理程序
End Sub
Public Sub btnItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'在這里編寫控件數(shù)組對點擊事件的響應(yīng)
'例如:
MsgBox("點擊:" sender.GetType().ToString CType(CType(sender, Label).Tag, String))
End Sub
End Class
使用創(chuàng)建的Label控件
在Form1中放置兩個按鈕Label1、Label2
雙擊Form添加代碼:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗體設(shè)計器生成的代碼 "
Public Sub New()
MyBase.New()
'該調(diào)用是 Windows 窗體設(shè)計器所必需的。
InitializeComponent()
'在 InitializeComponent() 調(diào)用之后添加任何初始化
'用來綁定label
BindArray()
End Sub
……Windows窗體設(shè)計器生成的其他代碼……
#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
然后可以測試點擊兩個label可以顯示相應(yīng)的Tag的信息。
布局方面,建議你用FlowLayoutPanel,不用手工控制控件位置了。假設(shè)有一個FlowLayoutPanel1。
先在窗體類中建一個控件的集合:
Dim
TextBoxes
As
New
List(Of
TextBox)
在button1的
Click事件
中寫:
For
i
=
1
To
5
'5是每次加入的數(shù)量
Dim
newTextBox
As
New
TextBox()
'newTextBox.Size
=
New
Size(100,20)
'
TextBoxes.Add(newTextBox)
'加入我們的集合中
FlowLayoutPanel1.Controls.Add(newTextBox)
'加到窗體的FlowLayoutPanel中去
Next
i
然后button2的Click事件中:
For
Each
itemTextBox
As
TextBox
In
TextBoxes
'這里的itemTextBox.Text就代表每個TextBox里的文本
'如果你需要清空,調(diào)用TextBoxes.Clear()和FlowLayoutPanel.Controls.Clear()即可
Next
Private Sub b_click(sender As Object, e As EventArgs)
MsgBox(sender.name)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim b() As Button = {Button1, Button2, Button3}
For Each i In b
AddHandler i.Click, AddressOf b_click
Next
End Sub
Dim Str(20) As String '20數(shù)據(jù)元素
Dim Text(20) As TextBox
Dim n As Integer = 0
For Each i In Text
i = New TextBox '實例化
i.Location = New Point(50, n * 20)
Me.Controls.Add(i) '添加窗體
Dim Index As Integer = i.TabIndex '沒index屬性
Str(n) = Index
i.Text = Str(n) 'str數(shù)組值賦值給文本框
n += 1
Next
對于窗體中的容器控件,比如 GroupBox,Panel 等,它僅僅訪問了這些控件本身,而不會去訪問它們的子控件。為了實現(xiàn)你所要求的功能,需要將上述代碼修改:
Public Sub IterateThroughControls(ByVal parent As Control)
For Each c As Control In parent.Controls
MessageBox.Show(c.ToString())
If c.HasChildern Then
'利用遞歸實現(xiàn)容器子控件的訪問,重復(fù)循環(huán)遍歷控件,調(diào)用自身函數(shù)
IterateThroughControls(c)
End If
Next
End Sub
調(diào)用方法:
IterateThroughControls(TabControl1) 'tabcontrol1為控件名,也可用groupbox1等
此外,判斷遍歷的控件是不是相應(yīng)的文本控件,或者combobox控件,可用此代碼判斷
For Each t As Control In Me.Controls
If TypeOf t Is TextBox Then
MsgBox(t)
End If
Next