vb.net For Each 在特定文本框上循环

vb.net For Each loop on specific textboxes

我有一个 ComponentOne 输入面板 'inputPanel' 从数据库中提取记录,

我想验证除名称文本框之外的所有框 'txtNAME',

'check' 是一个测试没有特殊字符的函数

For Each ctrl As Control In inputPanel.Controls

            If ctrl.Name <> "txtNAME" Then
                check= ctrl.Text
                If NumText(check) = False Then
                    MessageBox.Show("You can only Enter Numbers and Text for this field", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    ctrl.Focus()
                    Exit Sub
                End If
            End If
        Next

它仍在检查 txtName 的特殊字符

有什么帮助吗?

将循环限制为仅文本框,丢弃同一控件集合中的标签或按钮等控件

For Each txtBox In inputPanel.Controls.OfType(Of TextBox)
    If txtBox.Name <> "txtNAME" Then
        check= txtBox.Text
        If NumText(check) = False Then
            .....
        End If
    End If
Next

您也可以使用

丢弃不需要的文本框
For Each txtBox In inputPanel.Controls _
        .OfType(Of TextBox) _
        .Where(Function(x) x.Name <> "txtNAME")
   check= txtBox.Text
   If NumText(check) = False Then
        .....
   End If
Next

在不丢弃非 TextBox 控件的情况下,您循环还将面板控件集合内的每个标签传递给 NumText 方法,并且可能是触发错误消息的标签文本,而不是 txtNAME 框-