使用If语句停止下一步加载。 VB。编辑:年龄检查现在不起作用

时间:2013-12-09 04:52:41

标签: vb.net if-statement multiple-forms

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

编辑:感谢您的所有帮助,但现在年龄检查无效,因此您可以输入您想要的任何年龄,但不会显示错误。有任何想法吗。我会把所有修改后的代码放在最后

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

结束班级

5 个答案:

答案 0 :(得分:0)

请勿使用GoTo,而是使用Exit Sub()

答案 1 :(得分:0)

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

答案 2 :(得分:0)

将所有GoTo A更改为Exit Sub以打破子例程

答案 3 :(得分: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

答案 4 :(得分:0)

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

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

首先,检查txtAge.Text是否为数字。如果是,则将其解析为相同的String。为什么?它已经是数字了。嗯,实际上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,几乎不需要它。