Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检测空文本框的vb.net文本框标记_Vb.net - Fatal编程技术网

检测空文本框的vb.net文本框标记

检测空文本框的vb.net文本框标记,vb.net,Vb.net,我想做一个项目,将检测哪个文本框是空的,而不是空的 If txtInterviewee.Text = String.Empty Or txtInterviewed.Text = String.Empty Or txtValidated.Text = String.Empty Or _ txtResidence.Text = String.Empty Or dtpValidated.Text = String.Empty Or txtLastName.Text = Str

我想做一个项目,将检测哪个文本框是空的,而不是空的

If txtInterviewee.Text = String.Empty Or txtInterviewed.Text = String.Empty Or txtValidated.Text = String.Empty Or _
             txtResidence.Text = String.Empty Or dtpValidated.Text = String.Empty Or txtLastName.Text = String.Empty Or _
             txtFirstName.Text = String.Empty Or txtMiddleName.Text = String.Empty Or txtAddress.Text = String.Empty Or _
             cmbGender.Text = String.Empty Or cmbCivilStatus.Text = String.Empty Or dtpBirthDay.Text = String.Empty Or _
             txtBirthPlace.Text = String.Empty Or txtCitizenship.Text = String.Empty Then
            MsgBox("Must fill the following Fields")
        End If

如果任何文本框为空,我希望对其进行标记或其他操作,以便用户知道需要填充哪个文本框。

您可以遍历表单中的所有文本框,并检查它们是否为空。大概是这样的:

 For Each box As TextBox In Me.Controls.OfType(Of TextBox)()
     If box.Text = String.Empty Then
         box.Text = "This is empty!!"
     End If
 Next

你可以写一个这样的函数

Function CheckIfBoxEmpty(ByVal txt as TextBox) as Boolean
     if string.IsNullOrWhiteSpace(txt.Text) Then
         ' Optionally insert a message here .....
         txt.Focus()
         return True
     else
         return False
     End If
End Function
然后,您可以为要监视的每个文本框调用此函数

Dim isEmpty as Boolean = False
if Not isEmpty Then isEmpty = CheckIfBoxEmpty(txtInterviewee)
if Not isEmpty Then isEmpty = CheckIfBoxEmpty(txtInterviewed)
if Not isEmpty Then isEmpty = CheckIfBoxEmpty(txtValidated)
...and so on for the other textboxes
如果您有一些文本框需要选中,而有些文本框不需要选中,则建议使用上面的代码。相反,如果要选中表单上的所有文本框,则更适合使用简单的for each循环

For Each tbox in Me.Controls.OfType(of TextBox)
     if CheckIfBoxEmpty(tbox) Then
         Exit For
     End If
Next

通过这些方式,CheckIfBoxEmpty函数将焦点设置为空文本框,并返回True,表示已找到空文本框,您可以停止搜索。如果您确实需要向用户发送消息,您可以在设置文本框焦点之前添加该消息,使用MessageBox或更好的方法是在出现故障的文本框旁边闪烁的ErrorProvider对象。

查看ErrorProvider组件或从循环中提取消息