Vb.net 在visual basic 2012中,在单个按钮中同时验证文本框、组合框、单选按钮和日期时间选择器
你好!我有10个文本框,4个单选按钮,2个组合框,2个日期-时间选择器和1个按钮。这些对象用于接受来自用户的信息。我的难题是如何在单击1按钮时验证所有这些对象。例如,我想检查是否所有对象都已填充,如果没有,它将显示一条错误消息,指定用户未能输入的对象。但是,如果所有对象都已使用/填充,它将转移到另一个窗体并关闭另一个窗体。请帮助我:(如果您使用的是WinForms,您可以向按钮添加如下代码:Vb.net 在visual basic 2012中,在单个按钮中同时验证文本框、组合框、单选按钮和日期时间选择器,vb.net,validation,Vb.net,Validation,你好!我有10个文本框,4个单选按钮,2个组合框,2个日期-时间选择器和1个按钮。这些对象用于接受来自用户的信息。我的难题是如何在单击1按钮时验证所有这些对象。例如,我想检查是否所有对象都已填充,如果没有,它将显示一条错误消息,指定用户未能输入的对象。但是,如果所有对象都已使用/填充,它将转移到另一个窗体并关闭另一个窗体。请帮助我:(如果您使用的是WinForms,您可以向按钮添加如下代码: Private Sub Button1_Click(sender As Object, e As Eve
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim IsErrorPresent As Boolean = False ' We use this to flag whether ANY errors are present
Dim ErrorDescription As String = "" ' We use this to build our error message to the user
' Validate first control
If TextBox1.Text.Trim.Equals("") Then
IsErrorPresent = True ' We found an error!
ErrorDescription += "The first textbox is empty. Please fill this out." & vbNewLine
End If
' ... Do similar validation for other controls
' ... If any are found, we set IsErrorPresent to TRUE and add to our Error Description
' Now that we're done validating all of our controls, we either notify the user or process the valid data
If IsErrorPresent = True Then
MessageBox.Show("There is one or more errors with the information you supplied." & vbNewLine & vbNewLine & "Please see below: " & vbNewLine & vbNewLine & ErrorDescription, "Error In Form", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
' We only get here if there is no error present in controls. Therefore, we can process validated information
End If
End Sub
也许有更有效的方法可以做到这一点,但这应该是可行的