0

我有以下代码:

    For Each control2 As Control In GroupBox3.Controls
        If TypeOf control2 Is TextBox Then
            Dim txt2 As TextBox = TryCast(control2, TextBox)
            If counter > totalBoxes Then
                totalBoxes += 1
                txt2.Text = grade(totalBoxes)
            End If
        End If
    Next

我正在做的是遍历 groupbox3 中的每个文本框。然后检查计数器(表格中输入的总成绩)是否大于 totalBoxes(设置为 0),最后我将实际成绩(A、B、C、D)放入文本框中。问题是它出于某种原因在文本框 8(我有 10 个文本框)开始循环并从那里开始。有谁知道我的代码有什么问题?

亚伦

4

2 回答 2

2

好吧,您的文本框的名称与其在其父级的 .Controls 集合中的索引无关。

您可以做的一件事是将每个控件的 .tag 属性设置为您想要传递给您的成绩函数的索引。

textbox1.Tag = 1
textbox2.Tag = 2
...

这样您就不必担心迭代时文本框的顺序。

于 2011-07-10T02:31:56.107 回答
0
For Each box As TextBox In GroupBox3.Controls.OfType(Of TextBox).OrderBy(Function(t) t.Name)
    If counter > totalBoxes Then
        totalBoxes += 1
        box.Text = grade(totalBoxes)
    End If
Next box
于 2012-03-09T14:35:51.920 回答