Visual Basic迭代启用Textbox的

时间:2016-10-19 15:30:53

标签: vb.net iteration

我正在进行一项任务,我有10个RadioButtons,表明我有多少竞赛者,根据我在1到10之间选择的内容,我需要启用许多相应的TextBox,这样我才能填充它有名字!

有没有办法让我在1和我从RadioButton中挑选的数字之间进行For循环并说出类似

的内容
For i = 0 to Size
{
    TextBox&i.Enabled = True
}

因为我的TextBox被称为TextBox1到TextBox10

我知道您可以使用&添加字符串,但是如何为对象名称添加?

截至目前,我确实有最愚蠢的方式,这是每个RadioButton内部的点击事件,手动启用正确数量的TextBox ...

提前谢谢!

2 个答案:

答案 0 :(得分:1)

您可以迭代所有控件,如下所示:

For Each ctr In Me.Controls
Dim indx As String = ctr.Name
If TypeOf (ctr) Is Textbox Then
 ' Now compare the name with TextBox&i and do smth
End If
Next

答案 1 :(得分:1)

It's not possible to just concatenate a string and use it as an object variable reference like that, but you can search the form's controls by their name property (which is a string) and do it that way. Here's an example:

Private Sub EnableTextBoxes(ByVal Size As Integer)
    For i As Integer = 1 To Size
        Dim matches() As Control = Me.Controls.Find("Textbox" & i.ToString, True)
        If matches IsNot Nothing AndAlso matches.Length = 1 Then matches(0).Enabled = True
    Next
End Sub