我不知道 VB.NET,但我在 C# 中编写了这段代码,它检查 a 是否TabPage
包含TextBox
空的 a。如果您知道那种语言,我认为将它翻译成 VB.NET 很容易。
这是检查 TabPage 是否包含空 TextBox 的函数。该函数接收 TabPage 作为其参数并返回true
or false
。
private bool ContainsEmptyTextBox(TabPage tp)
{
bool foundTextBox = false;
bool textBoxIsEmpty = false;
foreach (Control c in tp.Controls)
{
if (c is TextBox)
{
foundTextBox = true;
TextBox tb = c as TextBox;
if (String.IsNullOrEmpty(tb.Text))
{
textBoxIsEmpty = true;
}
break;
}
}
if (foundTextBox == true && textBoxIsEmpty == true)
return true;
else
return false;
}
以下是如何使用该函数遍历 a 中的所有选项卡TabControl
并查看哪个选项卡包含空文本框:
private void button1_Click(object sender, EventArgs e)
{
foreach (TabPage tp in tabControl1.TabPages)
{
if (ContainsEmptyTextBox(tp))
{
// This tabpage contains an empty textbox
MessageBox.Show(tabControl1.TabPages.IndexOf(tp) + " contains an empty textbox");
}
}
}
编辑:我使用这个站点将 C# 代码自动转换为 VB.NET。
Private Function ContainsEmptyTextBox(tp As TabPage) As Boolean
Dim foundTextBox As Boolean = False
Dim textBoxIsEmpty As Boolean = False
For Each c As Control In tp.Controls
If TypeOf c Is TextBox Then
foundTextBox = True
Dim tb As TextBox = TryCast(c, TextBox)
If [String].IsNullOrEmpty(tb.Text) Then
textBoxIsEmpty = True
End If
Exit For
End If
Next
If foundTextBox = True AndAlso textBoxIsEmpty = True Then
Return True
Else
Return False
End If
End Function
Private Sub button1_Click(sender As Object, e As EventArgs)
For Each tp As TabPage In tabControl1.TabPages
If ContainsEmptyTextBox(tp) Then
' This tabpage contains an empty textbox
MessageBox.Show(tabControl1.TabPages.IndexOf(tp) & " contains an empty textbox")
End If
Next
End Sub