如何在 VB.NET 中制作按钮的控件数组?就像在 Visual Basic 6.0 中一样...
语法有可能像下面这样吗?
dim a as button
for each a as button in myForm
a.text = "hello"
next
.NET 中的控件只是普通对象,因此您可以自由地将它们放入普通数组或列表中。不再需要特殊的 VB6 控制数组结构。
所以你可以说,
Dim buttons As Button() = { Button1, Button2, … }
For Each button As Button In Buttons
button.Text = "foo"
End For
或者,您可以直接迭代容器内的控件(例如表单):
For Each c As Control In MyForm.Controls
Dim btt As Button = TryCast(c, Button)
If btt IsNot Nothing Then ' We got a button!
btt.Text = "foo"
End If
End For
请注意,这仅适用于以下控件directly表格上;嵌套到容器中的控件不会以这种方式迭代;但是,您可以使用递归函数来迭代所有控件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)