Using If Statements to stop loading next from. VB. Edit: Age check now not working

1.3k views Asked by At

I'm trying to use message boxes to stop a user from continuing to the next form but not really sure what's going wrong. The message box pops up but still changes to the next form. Any help please?

Edit: Thanks for all your help but now the age check isn't working so you can enter any age you want and it doesn't show an error. Any ideas. I'll put all altered code at the end

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

End Class*

5

There are 5 answers

0
Victor Zakharov On

Your code has quite a few logical errors, for example:

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

First, you check that txtAge.Text is numeric. If yes, you parse it into the same String. Why? It's already numeric. Well, actually IsNumeric can also mean a decimal. But there is no point in doing parsing twice, instead of just one time. Then, if it's not numeric, you proceed with ElseIf. Suppose strAge = "AAA", now you are doing a string comparison "AAA" < 18. Why it's bad, see below.

I am pretty sure you have Option Strict Off, because otherwise compiler would shout at you at this very line. Problem is that your comparison implicitly becomes "AAA" < "18", which is False (you can use Immediate Window to check). However, when it reaches "AAA" > "125", this statement is actually True. And you get this message: Don't be stupid. You're not that old., which is irrelevant to the input. Age check will never work like that. Please do yourself a favour and turn on Option Strict On, either on a per file basis, or project-wide. You will see how many places of your code have code smell.

If you want to get working code, please describe in words (not code) what you want your code to do, and I'll try to come up with a code sample for you, following the best practices, so you can learn from it. And yeah, don't use GoTo's, there is hardly ever a need for it.

0
OSKM On

I take it you still want to display the final error message before shutting the form down?

Your current issue is that although the error message is displayed there is nothing to break the code at that point so it still runs the statement to display the next form (frmCreditCardInfo.Show()), a solution to this is as below.

Change:

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

to:

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

You probably also want to drop a few (or all) of the GoTo A statements as they will send the code directly there without allowing the errorcount to build up enough to show the error message box (alternatively if you want these to be an immediate failure set the errorcount = 5 before the GoTo A statement.

0
Justin E On

Don't use GoTo, Instead use Exit Sub()

0
MusicLovingIndianGirl On

Use Exit Sub - It breaks your subroutine's execution.

0
Codemunkeee On

Change all GoTo A to Exit Sub to break the subroutine