我正在创建一个用于计算学校课程作业的帐户检索程序,该程序检查输入的电子邮件地址和密码与存储在CSV文件中的程序,如果输入正确则程序显示其用户信息,如果输入是不正确它会输出一条错误消息告诉他们。我已经在Visual Basic中创建了程序并且它工作正常,除非我按下提交而不在输入字段中输入任何内容。如果未输入任何内容,程序将调用错误消息以进行无输入,并输出错误消息以查看无效/错误输入。我该如何解决?我的代码如下:
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 (Not String.IsNullOrWhiteSpace(EmailAddress.Text)) Then
If EmailAddress.Text = currentRow(0) And Password.Text = currentRow(1) Then
Me.InfoDisplay.Rows.Add(currentRow)
foundmatch = True
Exit While
End If
Else 'null/empty or white space
MsgBox("You need to type something into the input boxes")
foundmatch = False
Exit While
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
Private Sub Reset_Click(sender As Object, e As EventArgs) Handles Reset.Click
EmailAddress.Text = Nothing
Password.Text = Nothing
InfoDisplay.Rows.Clear()
End Sub
End Class
答案 0 :(得分:0)
当您输入任何内容时,您将在csv中第一行的循环(空/空或空格)循环中以你的else情况结束,因此消息框"您需要输入进入输入框的东西"将显示并且另外foundmatch将为False,这将导致"输入的电子邮件和/或密码无法找到或不正确。"显示。
要解决此问题,我建议将空格检查移出While循环,如果输入为空则跳过所有其他操作。