【发布时间】:2020-11-18 11:00:34
【问题描述】:
我正在编写一个程序,允许用户输入个人详细信息,但每个详细信息都受到字符长度或类似内容的限制。我需要它来允许用户在被清除并发送回之前的表单之前 5 次输入错误的详细信息。我目前正在尝试为此使用循环,但是当您按下输入详细信息按钮时,它会立即循环 5 次并将用户返回,而用户无法更改详细信息并重试。这是我目前所拥有的。
Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
'Declare variables
'This is the form that i will only allow the operator to enter 5 mistakes before being locked out
Dim strStreetAddress As String = txtStreetAddress.Text
Dim strTown As String = txtTown.Text
Dim strCounty As String = txtCounty.Text
Dim strEircode As String = txtEircode.Text
Dim mskPhoneNumber As String = mskNumber.Text
Dim intErrorCount As Integer = 0
'set up if statements
Do Until intErrorCount = 5
If strStreetAddress.Length = 0 Or strTown.Length = 0 Or strCounty.Length = 0 Or mskPhoneNumber.Length = 0 Or strEircode.Length = 0 Then
intErrorCount += 1
MessageBox.Show("One or more details have not been enterred")
txtStreetAddress.Clear()
txtCounty.Clear()
txtEircode.Clear()
txtTown.Clear()
mskNumber.Clear()
ElseIf strStreetAddress.Length > 50 Or strTown.Length > 15 Or strCounty.Length > 10 Or strEircode.Length <> 7 Or mskPhoneNumber.Length <> 10 Then
intErrorCount += 1
MessageBox.Show("One or more detais has been entered incorrectly")
txtStreetAddress.Clear()
txtCounty.Clear()
txtEircode.Clear()
txtTown.Clear()
mskNumber.Clear()
Else MessageBox.Show("All Details Entered Correctly")
Me.Hide()
frmPurchaseScreen.Show()
End If
Loop
'if errorcount reaches 5 then operator must login again and restart
If intErrorCount = 5 Then
MessageBox.Show("You have made too many errors, you must Login again")
Me.Hide()
frmLogin.Show()
End If
End Sub
Private Sub txtErrorCount_TextChanged(sender As Object, e As EventArgs) Handles txtErrorCount.TextChanged
End Sub
End Class
【问题讨论】:
-
是否有任何理由将错误输入限制为 5 次? WinForms 中有一个验证概念。在此answer中查找示例
标签: vb.net loops if-statement