我正在用Visual Basic编写一个程序,当用户输入他们的电子邮件和密码时(从CSV文件中的电子邮件和密码列表中),它将为他们提供他们的姓名,地址,密码等。我已经成功实现了然而,当我在该字段中没有输入任何内容时,程序只输入5个空白行,我该如何解决这个问题呢?我需要一条错误消息说没有输入任何内容并拒绝它。我该怎么做呢?我的代码如下:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Password.Click
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles EmailAddress.Click
End Sub
Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
Dim currentRow As String()
Dim foundmatch As Boolean
Using parserDetails As New Microsoft.VisualBasic.FileIO.TextFieldParser("CSV_File.csv")
parserDetails.SetDelimiters(",")
While Not parserDetails.EndOfData
currentRow = parserDetails.ReadFields()
If EmailAddress.Text = currentRow(0) And Password.Text = currentRow(1) Then
Me.DataGridView1.Rows.Add(currentRow)
foundmatch = True
End If
End While
End Using
If Not foundmatch Then
MsgBox("The email and/or password entered cannot be found or is incorrect.", MsgBoxStyle.Critical, "Invalid Email")
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Input(p1 As String)
Throw New NotImplementedException
End Sub
End Class
答案 0 :(得分:1)
您将使用If
构造来检查条件。例如......
Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
If String.IsNullOrWhiteSpace(EmailAddress.Text) Then
MsgBox('Email Address is required!')
Return
End If
' The rest of your logic
End Sub
您可以一次检查多个条件,按顺序等。
几点说明:
DataTable
?)中,那么你可以执行更有效的搜索而不仅仅是扫描逐行。 (或者,如果你可以使用实际的数据库,甚至更好。)对于少量数据,这可能不是什么大问题,但扩展到更大的数量将有所作为。答案 1 :(得分:0)
你想要使用这样的东西:
If(Not String.IsNullOrWhitespace(EmailAddress.Text)) Then
'put
Else 'null/empty or white space
Msgbox("You cannot leave the email address empty...")
End If
即。你可以改变你的while块:
While Not parserDetails.EndOfData
currentRow = parserDetails.ReadFields()
If (Not String.IsNullOrWhiteSpace(EmailAddress.Text)) Then
If EmailAddress.Text = currentRow(0) And Password.Text = currentRow(1) Then
Me.DataGridView1.Rows.Add(currentRow)
foundmatch = True
Exit While
End If
Else 'null/empty or white space
MsgBox("You cannot leave the email address empty...")
foundmatch = False
Exit While
End If
End While
请注意添加Exit While
s,这样可以提高效率,因为在找到正确的值后不再需要循环。