我有一个包含6个组框和120个文本框的表单。在数据更新到数据库期间,我希望空文本框返回值" -NA - "。我尝试了以下代码,但我一直收到错误:
For i As Integer = 1 To 20
If GroupBox1.Controls("CollarTB" & i).Text = String.Empty Then
GroupBox1.Controls("CollarTB" & i).Text = "-NA-"
End If
If Me.GroupBox1.Controls("CollarTB" & i).Text = String.Empty Then
Me.Controls("CollarTB" & i).Text = "-NA-"
End If
If Form9.Controls(i).Text = String.Empty Then
Form9.Controls(i).Text = "-NA-"
End If
Next
If FrntTB1.Text = String.Empty Then FrntTB1.Text = "-NA-"
如何做到这一点?有什么解决方案吗?
另外,我还有一个包含大约180个文本框的组合框。我想一次从一行数据库中填充它们。可能吗?如果是这样,怎么样?
答案 0 :(得分:0)
您可以使用linq
过滤groupboxes
中的form
,groupboxes
可以过滤掉textboxes
,然后只需设置值根据您的要求textboxes
。试试这段代码,
Linq Type I:
For Each xGroupBox As GroupBox In Me.Controls.OfType(Of GroupBox)()
For Each xTextBox As TextBox In xGroupBox.Controls.OfType(Of TextBox)()
If xTextBox.Name.StartsWith("CollarTB") andalso xTextBox.text = string.empty Then
xTextBox.Text = "-NA-"
End If
Next
Next
Linq Type II:
For Each xGroupBox As GroupBox In Me.Controls.OfType(Of GroupBox)()
For Each xTextBox As TextBox In From xTxtbx In xGroupBox.Controls.OfType(Of TextBox)() Where xTxtbx.Text.StartsWith("CollarTB") and xTxtbx.text = string.empty
xTextBox.Text = "-NA-"
Next
Next
Non Linq版本:
For Each xControls As Control In Me.Controls
If TypeOf xControls Is GroupBox Then
For Each xControls1 As Control In xControls.Controls
If TypeOf xControls1 Is TextBox Then
If xControls1.Name.StartsWith("CollarTB") andalso xControls1.text = string.empty Then
xControls1.Text = "-NA-"
End If
End If
Next
End If
Next