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



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

问题是我在这个盒子上有多种类型的控件,包括:文本框、数字上下和日期选择器。

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

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

我在B1emptyTextBoxes, B1emptyNumericBox消息框中显示标签时也遇到了问题,因为我得到这个字符串而不是标签

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

任何建议将不胜感激。

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

If GroupBox1.Text = "1" Then

接下来,一个 .标记属性的数据类型为对象。你可以把任何你想要的东西扔进去。但是,当您引用它时,您将获得一个对象。当您致电 .对象上的ToString(这是您的String.Format正在执行的操作(,您将获得对象的完全限定名称。

CStr(( 将其更改回底层类型字符串。如果你把光标放在B1emptyTextBox和B1emptyNumericBox上,用于添加CStr((,你会看到数据类型是Object的IEnumerable。添加 CStr(( 后,您将看到字符串的 IEnumerable。

然后将 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

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

您的MessageBox字符串将需要:

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

最新更新