检查组框内的多个控件类型是否为空,并显示空控件的标签

Check if multiple control types are empty inside groupbox and display tags of empty controls

如前所述,我需要检查组框中的所有字段是否为空,如果为空,则显示一个消息框,显示空字段的标签。

问题是我在此框上有多种类型的控件,包括:TextBox、NumericUpDown 和 DatePicker。

我想知道是否有比我在下面做的更简单的方法。

        If BenchQtyCombo.Text = 1 Then
            Dim B1emptyTextBoxes =
            From txt In Bench1_Combo.Controls.OfType(Of TextBox)()
            Where txt.Text.Length = 0
            Select txt.Tag

            Dim B1emptyNumericBox =
            From num In Bench1_Combo.Controls.OfType(Of NumericUpDown)()
            Where num.Value = 0
            Select num.Tag

            If B1emptyTextBoxes.Any Or B1emptyNumericBox.Any Then
                MessageBox.Show(String.Format("Please Complete the Following Required Fields:" & vbCrLf & vbCrLf & "{0}", String.Join(", ", B1emptyTextBoxes, B1emptyNumericBox)))

            End If
        End If

当我收到 this string instead of the tags

时,我也遇到 B1emptyTextBoxes, B1emptyNumericBox 在消息框中显示标签的问题

我还没有包含日期​​选择器的代码,它类似于 Where DatePicker.Date > Today 代码,直到我开始工作。

如有任何建议,我们将不胜感激。

您正在加入 2 个列表以在 MessageBox 中显示它们。您需要分别加入这些列表的内容,然后再加入这些字符串。

您的 MessageBox 字符串需要:

String.Join(", ", String.Join(", ", B1emptyTextBoxes), String.Join(", ", B1emptyNumericBox))

首先,分组框的文本 属性 永远不能等于 1。1 是数字类型。通常文本 属性 包含一个字符串。

If GroupBox1.Text = "1" Then

接下来,.Tag 属性 是数据类型对象。你可以把任何你想要的东西扔进去。但是,当您引用它时,您将获得一个对象。当您在一个对象上调用 .ToString 时(这是您的 String.Format 正在做的事情),您将获得该对象的完全限定名称。

CStr() 将其改回基础类型 String。如果在添加 CStr() 之前将光标悬停在 B1emptyTextBoxes 和 B1emptyNumericBox 上,您将看到数据类型是 IEnumerable of Object。添加 CStr() 后,您将看到 IEnumerable of String.

然后将 2 个 IEnumerables 添加到 Concat() 方法和 Bob 是你的叔叔。

Private Sub OPCode()
    If GroupBox1.Text = "GroupBox1" Then
        Dim B1emptyTextBoxes = From txt In GroupBox1.Controls.OfType(Of TextBox)()
                               Where txt.Text.Length = 0
                               Select CStr(txt.Tag)

        Dim B1emptyNumericBox = From num In GroupBox1.Controls.OfType(Of NumericUpDown)()
                                Where num.Value = 0
                                Select CStr(num.Tag)


        If B1emptyTextBoxes.Any Or B1emptyNumericBox.Any Then
            Dim BothEnumerables = B1emptyTextBoxes.Select(Function(s) s).Concat(B1emptyNumericBox.Select(Function(s) s))
            MessageBox.Show(String.Format("Please Complete the Following Required Fields:" & vbCrLf & vbCrLf & "{0}", String.Join(", ", BothEnumerables)))
        End If
    End If
End Sub