Dim n As Integer = 0
For Each i In Text
i = New TextBox '实例化
Me.Controls.Add(i) '添加窗体
Dim Index As Integer = i.TabIndex '没index属性
Str(n) = Index
i.Text = Str(n) 'str数组值赋值给文本框
n += 1
Next
对于窗体中的容器控件,比如 GroupBox,Panel 等,它仅仅访问了这些控件本身,而不会去访问它们的子控件.为了实现你所要求的功能,需要将上述代码修改:
Public Sub IterateThroughControls(ByVal parent As Control)
For Each c As Control In parent.Controls
MessageBox.Show(c.ToString())
If c.HasChildern Then
'利用递归实现容器子控件的访问,重复循环遍历控件,调用自身函数
IterateThroughControls(c)
End If
End Sub
调用方法:
IterateThroughControls(TabControl1) 'tabcontrol1为控件名,也可用groupbox1等
此外,判断遍历的控件是不是相应的文本控件,或者combobox控件,可用此代码判断
For Each t As Control In Me.Controls
If TypeOf t Is TextBox Then
MsgBox(t)
似有控件遍历窗体控件集合,或在控件集合中查询该控件名字,公用控件直接名字
Private Sub b_click(sender As Object, e As EventArgs)
MsgBox(sender.name)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each i In b
AddHandler i.Click, AddressOf b_click
子控件集合.
.controls
第一个子控件的子控件集合.
.controls(0).controls
以上就是土嘎嘎小编为大家整理的vb.net控件集合相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!