![](/img/trans.png)
[英]How to check the controls (display name and type) in the GroupBox by recursion and display on a DataGridView?
[英]Check if multiple control types are empty inside groupbox and display tags of empty controls
如前所述,我需要檢查 groupbox 中的所有字段是否為空,然后顯示一個消息框,顯示空字段的標簽。
問題是我在這個盒子上有多種類型的控件,包括: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
我也有B1emptyTextBoxes, B1emptyNumericBox
的問題, B1emptyTextBoxes, B1emptyNumericBox
在消息B1emptyTextBoxes, B1emptyNumericBox
顯示標簽,因為我得到的是這個字符串而不是標簽
我還沒有包含 datepicker 的代碼,它類似於Where DatePicker.Date > Today
代碼,直到我開始工作。
任何建議將不勝感激。
您正在加入 2 個列表以在MessageBox
顯示它們。 您需要單獨加入這些列表的內容,然后加入這些字符串。
您的MessageBox
字符串將需要:
String.Join(", ", String.Join(", ", B1emptyTextBoxes), String.Join(", ", B1emptyNumericBox))
首先,組框的文本屬性永遠不能等於 1。1 是數字類型。 通常文本屬性包含一個字符串。
If GroupBox1.Text = "1" Then
接下來,.Tag 屬性的數據類型為 Object。 你可以把任何你想要的東西扔進去。 但是,當您引用它時,您將獲得一個對象。 當您在對象上調用 .ToString 時(這是您的 String.Format 正在執行的操作),您將獲得對象的完全限定名稱。
CStr() 將其改回基礎類型 String。 如果在添加 CStr() 之前將光標懸停在 B1emptyTextBoxes 和 B1emptyNumericBox 上,您將看到數據類型是 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.