使用If语句停止从加载下一个.VB.Edit:年龄检查现在不起作用



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

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

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

结束类*

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

使用Exit Sub-它会中断子例程的执行。

将所有GoTo A更改为Exit Sub以中断子程序

我想你还想在关闭表单之前显示最后的错误消息吗?

您当前的问题是,尽管显示了错误消息,但此时没有任何内容可以中断代码,因此它仍然运行语句来显示下一个表单(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建立到足以显示错误消息框的程度(或者,如果您希望这些语句立即失败,请在GoTo A语句之前设置errorcount = 5

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

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,几乎不需要它。

相关内容

  • 没有找到相关文章

最新更新