0

我正在尝试使用消息框来阻止用户继续下一个表单,但不确定出了什么问题。消息框弹出,但仍更改为下一个表单。请问有什么帮助吗?

编辑:感谢您的所有帮助,但现在年龄检查不起作用,因此您可以输入您想要的任何年龄并且它不会显示错误。有任何想法吗。我会将所有更改的代码放在最后

Dim errorcount As Integer = 0

Private Sub btnContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnContinue.Click

    strName = txtName.Text
    strAddress = rtfAddress.Text
    strCity = txtCity.Text
    strEmail = txtEmail.Text
    strHomePhone = mtxtHomePhone.Text
    strMobilePhone = mtxtMobilePhone.Text

    If txtName.Text = "" Then
        MessageBox.Show("You must enter full name", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A

    ElseIf IsNumeric(txtName.Text) Then
        MessageBox.Show("Please enter a valid name", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    If txtAge.Text = "" Then
        MessageBox.Show("You must enter your age", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
        'Else
        '    strAge = Integer.Parse(txtAge.Text)

    End If

    'Declaring age check
    Dim AgeCheck As Boolean = False

    If IsNumeric(txtAge.Text) = True Then
        AgeCheck = True

        strAge = Integer.Parse(txtAge.Text)
    ElseIf strAge < 18 Then
        MessageBox.Show("You must be over 18 years old", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False

    ElseIf strAge > 125 Then
        MessageBox.Show("Don't be stupid. You're not that old.", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False

    ElseIf AgeCheck = False Then
        MessageBox.Show("You must enter a valid age", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
    End If

    If Not IsNumeric(txtAge.Text) Then
        MessageBox.Show("Please enter a vadid age", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    If IsNumeric(rtfAddress.Text) Or rtfAddress.Text = "" Then
        MessageBox.Show("Please enter your address", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    If IsNumeric(txtCity.Text) Or txtCity.Text = "" Then
        MessageBox.Show("Please enter your town/city", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    'if no index hasd been selected
    If cmbCounty.SelectedItem = "" Then
        MessageBox.Show("Please select a county", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    If Not mtxtHomePhone.MaskCompleted Then
        MessageBox.Show("Please enter a valid home phone number", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    If Not mtxtMobilePhone.MaskCompleted Then
        MessageBox.Show("Please enter a valid mobile phone number", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    If txtEmail.Text = "" Then
        MessageBox.Show("You must enter a valid email address", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A

    ElseIf Not txtEmail.Text.Contains("@") Then
        MessageBox.Show("Not a valid email address", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

A:
    If errorcount >= 5 Then
        MessageBox.Show("Too many errors. Shutting down", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Warning)
        End
    End If

        Me.Hide()
        frmCreditCardInfo.Show()

End Sub

*Dim errorcount As Integer = 0

Private Sub btnContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnContinue.Click
    strName = txtName.Text
    strAddress = rtfAddress.Text
    strCity = txtCity.Text
    strEmail = txtEmail.Text
    strHomePhone = mtxtHomePhone.Text
    strMobilePhone = mtxtMobilePhone.Text
    If txtName.Text = "" Then
        MessageBox.Show("You must enter full name", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    ElseIf IsNumeric(txtName.Text) Then
        MessageBox.Show("Please enter a valid name", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    If txtAge.Text = "" Then
        MessageBox.Show("You must enter your age", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    'Declaring age check
    Dim AgeCheck As Boolean = False
    If IsNumeric(txtAge.Text) = True Then
        AgeCheck = True
        'strAge = Integer.Parse(txtAge.Text)
    ElseIf strAge < 18 Then
        MessageBox.Show("You must be over 18 years old", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
    ElseIf strAge > 125 Then
        MessageBox.Show("Don't be stupid. You're not that old.", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
    ElseIf AgeCheck = False Then
        MessageBox.Show("You must enter a valid age", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        Exit Sub
    End If
    If Not IsNumeric(txtAge.Text) Then
        MessageBox.Show("Please enter a vadid age", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
    End If
    If IsNumeric(rtfAddress.Text) Or rtfAddress.Text = "" Then
        MessageBox.Show("Please enter your address", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    If IsNumeric(txtCity.Text) Or txtCity.Text = "" Then
        MessageBox.Show("Please enter your town/city", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    'if no index hasd been selected
    If cmbCounty.SelectedItem = "" Then
        MessageBox.Show("Please select a county", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    If Not mtxtHomePhone.MaskCompleted Then
        MessageBox.Show("Please enter a valid home phone number", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    If Not mtxtMobilePhone.MaskCompleted Then
        MessageBox.Show("Please enter a valid mobile phone number", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    If txtEmail.Text = "" Then
        MessageBox.Show("You must enter a valid email address", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
    ElseIf Not txtEmail.Text.Contains("@") Then
        MessageBox.Show("Not a valid email address", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    If errorcount >= 5 Then
        MessageBox.Show("Too many errors. Shutting down", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Warning)
        Me.Dispose()
        Exit Sub
        'End
        'Exit Sub
    End If
    Me.Hide()
    frmCreditCardInfo.Show()
End Sub

结束课程*

4

5 回答 5

0

我认为您仍然想在关闭表单之前显示最终错误消息?

您当前的问题是,尽管显示了错误消息,但此时没有任何内容可以破坏代码,因此它仍然运行语句以显示下一个表单 ( frmCreditCardInfo.Show()),对此的解决方案如下。

改变:

A:
    If errorcount >= 5 Then
        MessageBox.Show("Too many errors. Shutting down", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Warning)
        End
    End If

    Me.Hide()
    frmCreditCardInfo.Show()

End Sub

至:

A:
    If errorcount >= 5 Then
        MessageBox.Show("Too many errors. Shutting down", "Error", MessageBoxButtons.OK, _
                    MessageBoxIcon.Warning)
        Me.Dispose()
        Exit Sub
    End If

    Me.Hide()
    frmCreditCardInfo.Show()

End Sub

您可能还希望删除一些(或全部)GoTo A语句,因为它们将直接将代码发送到那里,而不允许errorcount构建足够的代码以显示错误消息框(或者,如果您希望这些语句立即失败,请设置errorcount = 5之前GoTo A声明 。

于 2013-12-09T07:57:31.623 回答
0

不要使用 GoTo,而是使用Exit Sub()

于 2013-12-09T04:55:31.447 回答
0

使用Exit Sub- 它会破坏您的子程序的执行。

于 2013-12-09T04:56:17.210 回答
0

全部GoTo A改成Exit Sub中断子程序

于 2013-12-09T05:04:30.573 回答
0

您的代码有很多逻辑错误,例如:

If IsNumeric(txtAge.Text) = True Then
  AgeCheck = True
  strAge = Integer.Parse(txtAge.Text)
ElseIf strAge < 18 Then

首先,您检查它txtAge.Text是数字。如果是,则将其解析为相同的字符串。为什么?已经是数字了。好吧,实际上IsNumeric也可以表示小数。但是解析两次而不是一次解析是没有意义的。然后,如果它不是数字,则继续ElseIf. 假设strAge = "AAA",现在您正在进行字符串比较"AAA" < 18。为什么不好,见下文。

我很确定你有Option Strict Off,否则编译器会在这一行对你大喊大叫。问题是您的比较隐式变为"AAA" < "18",即False(您可以使用即时窗口进行检查)。然而,当它到达时"AAA" > "125",这个语句实际上是True。你会得到这个消息:Don't be stupid. You're not that old.,这与输入无关。年龄检查永远不会那样工作。请帮自己一个忙并打开Option Strict On,无论是在每个文件的基础上,还是在项目范围内。你会看到你的代码有多少地方有代码异味。

如果您想获得工作代码,请用文字(而不是代码)描述您希望代码做什么,我会尝试按照最佳实践为您提供代码示例,以便您从中学习. 是的,不要使用GoTo's,几乎不需要它。

于 2013-12-09T14:35:36.497 回答