dimanche 13 novembre 2016

My conditional statement isn't working

I am in an entry level visual basic class. I am trying to test user input to see if it us numeric using the IsNumeric boolean function. Basically, if the input is not numeric, I want it to throw up a messagebox saying so, and if it IS numeric, I want it to set the numeric values as variables. Every time I enter a non-numeric value, I don't get the messagebox, instead I get an exception when it tries to convert a non-numeric string to a double using CDbl. What am I doing wrong?

    If txtInches.Text = "" Or txtFeet.Text = "" Then 'Checks to ensure height fields were not left blank
        'If fields were blank, throws error message up, changes textbox backgroud color to red, focuses user on error and exits sub.
        MessageBox.Show("You must enter a value for both feet and inches. Don't leave these fields blank, and try again.", "Height Error", MessageBoxButtons.OK)
        txtFeet.BackColor = Color.Red
        txtInches.BackColor = Color.Red
        txtFeet.Focus()
        Exit Sub
    ElseIf txtAge.Text = "" Then 'Checks to see if age field was blank
        'If age field was blank, throws error message up, changes textbox backgroud color to red, focuses user on error and exits sub.
        MessageBox.Show("You must enter your age. Don't leave this field blank, and try again.", "Age Error", MessageBoxButtons.OK)
        txtFeet.BackColor = Color.Red
        txtInches.BackColor = Color.Red
        txtFeet.Focus()
        Exit Sub
    ElseIf Not IsNumeric(dblFeet) Then
        'If feet input is not numeric, throws error message up, changes textbox background color to red, focuses user on error and exits sub.
        MessageBox.Show("Feet must be a numeric value. Please try again.", "Height Error", MessageBoxButtons.OK)
        txtFeet.BackColor = Color.Red
        txtFeet.Focus()
        Exit Sub
    ElseIf Not IsNumeric(dblInches) Then 'Checks to see if height input is numeric
        'If inches input is not numeric, throws error message up, changes textbox background color to red, focuses user on error and exits sub.
        MessageBox.Show("Inches must be a numeric value. Please try again.", "Height Error", MessageBoxButtons.OK)
        txtInches.BackColor = Color.Red
        txtInches.Focus()
        Exit Sub
    ElseIf Not IsNumeric(dblAge) Then 'Checks to see if age input is numeric
        'If age input is not numeric, throws error message up, changes textbox background color to red, focuses user on error and exits sub.
        MessageBox.Show("Your age must be a number. Please try again.", "Age Error", MessageBoxButtons.OK)
        txtAge.BackColor = Color.Red
        txtAge.Focus()
        Exit Sub
    Else
        dblFeet = CDbl(txtFeet.Text)
        dblInches = CDbl(txtInches.Text)
        dblAge = CDbl(txtAge.Text)
    End If

Aucun commentaire:

Enregistrer un commentaire