【问题标题】:How do I get the programme to allow the user to change info if it is enterred incorrectly during a loop?如果在循环期间输入错误,如何让程序允许用户更改信息?
【发布时间】: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


【解决方案1】:

这是因为您在内部子声明了您的错误计数器,并且因为用户在您循环时无法更改他的输入。

如果你在 sub 中声明一个变量,它会在代码退出 sub 时消失。要将信息保留在 sub 之外,您需要在更高级别声明变量。

当用户点击并记住错误编号时,不要在调用的 sub 内循环,并使用更高级别的变量:

 Private intErrorCount As Integer = 0

    Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles Button1.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





        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


        '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

【讨论】:

  • 你是救生员!我一直在尝试很多不同的事情,不敢相信它是如此简单。谢谢!
  • 很高兴为您提供帮助 :) 我想这是因为您之前在做控制台应用程序。使用 winform,您必须考虑事件。
猜你喜欢
  • 2022-01-12
  • 2021-11-04
  • 1970-01-01
  • 1970-01-01
  • 2015-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多