Dim Str(20) As String '20數(shù)據(jù)元素
成都創(chuàng)新互聯(lián)公司2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元廣德做網(wǎng)站,已為上家服務(wù),為廣德各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
Dim Text(20) As TextBox
Dim n As Integer = 0
For Each i In Text
i = New TextBox '實(shí)例化
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
對(duì)于窗體中的容器控件,比如 GroupBox,Panel 等,它僅僅訪問了這些控件本身,而不會(huì)去訪問它們的子控件。為了實(shí)現(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
'利用遞歸實(shí)現(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
似有控件遍歷窗體控件集合,或在控件集合中查詢該控件名字,公用控件直接名字
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
子控件集合。
.controls
第一個(gè)子控件的子控件集合。
.controls(0).controls