Hi, can anyone look at this and see why it doesn't work? When I test and enter a numeric value in the text box it still gives the error I have placed in the code, same if I put in a letter etc.
Thanks!! :)

Private Sub valueDouble(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTestscore1.TextChanged, txtTestscore2.TextChanged, txtTestscore3.TextChanged


        'verify input are numeric

        Dim dblvalueDouble As Double
        Dim dblvalueDouble1 As Double
        Dim dblvalueDouble2 As Double



        If Not Double.TryParse(Me.txtTestscore1.Text, dblvalueDouble) OrElse Not Double.TryParse(Me.txtTestscore2.Text, dblvalueDouble1) Then
            MessageBox.Show("Invalid Entry please enter a number")
            txtTestscore1.Clear()
        ElseIf Not Double.TryParse(Me.txtTestscore3.Text, dblvalueDouble2) Then
            MessageBox.Show("Invalid Entry please enter a number")
            txtTestscore2.Clear()

        End If

    End Sub

Recommended Answers

All 6 Replies

Since u handle all 3 textboxes textChanged event with the same function and checking all 3 textboxes, you have atleast 2 textboxes where the text is "". converting an empty string to a double will always throw an exception.

Ok I changed it to this, and now when I input a numeric or letter I get the error. I don't understand what I am doing wrong. Is it my Private Sub line that is causing the problem?

Private Sub valueDouble(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTestscore1.TextChanged, txtTestscore2.TextChanged, txtTestscore3.TextChanged

        Dim dblscore As Double
        Dim dblscore2 As Double
        Dim dblscore3 As Double
        

        'verify inputs are numeric


        Double.TryParse(txtTestscore1.Text, dblscore)
        Double.TryParse(txtTestscore2.Text, dblscore2)
        Double.TryParse(txtTestscore3.Text, dblscore3)


        If IsNumeric(txtTestscore1.text) = False Then
            MessageBox.Show("Please enter a numeric value.")
            txtTestscore1.Focus()
        ElseIf IsNumeric(txtTestscore2.Text) = False Then
            MessageBox.Show("Please enter a numeric value.")
        ElseIf IsNumeric(txtTestscore3.Text) = False Then
            MessageBox.Show("Please enter a numeric value.")
        Else
        End If

You're testing it on the text changed event for each of the textboxes, but you're testing each textbox at the same time. Naturally, when you put a number in the first textbox but the second and third are still empty, those textboxes will fail the test and you'll get your error message.

Instead, try to work with only the specific textbox that caused the text changed event to fire. Use the sender object.

Private Sub valueDouble(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTestscore1.TextChanged, txtTestscore2.TextChanged, txtTestscore3.TextChanged

        Dim box As TextBox = CType(sender, TextBox)

        Dim inputValue As Double
        If Not (Double.TryParse(box.Text, inputValue)) Then
            MessageBox.Show("Please enter a numeric value.")
            box.Focus()
        End If

    End Sub

You're testing it on the text changed event for each of the textboxes, but you're testing each textbox at the same time. Naturally, when you put a number in the first textbox but the second and third are still empty, those textboxes will fail the test and you'll get your error message.

Instead, try to work with only the specific textbox that caused the text changed event to fire. Use the sender object.

Private Sub valueDouble(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTestscore1.TextChanged, txtTestscore2.TextChanged, txtTestscore3.TextChanged

        Dim box As TextBox = CType(sender, TextBox)

        Dim inputValue As Double
        If Not (Double.TryParse(box.Text, inputValue)) Then
            MessageBox.Show("Please enter a numeric value.")
            box.Focus()
        End If

    End Sub

Thanks for the reply. I have not used the sender before so I am a bit confused. How would a add the other boxes. I do understand now that it was trying to test all three boxes at once. Thanks for pointing that out.

Thanks for the reply. I have not used the sender before so I am a bit confused. How would a add the other boxes. I do understand now that it was trying to test all three boxes at once. Thanks for pointing that out.

The method I provided will test all three as you change the text within them. If you're changing txtTestscore3, that's the box it checks, because txtTestscore3 would be the sender on that particular event.

Your original method is one that would be good for testing each of the boxes after all inputs have been made; for instance, on a button click event that progresses you to the next step in the process.

Got it. Thank you so much. That makes much more sense to me now. Seems like sometimes you dig yourself a hole and can't get out :)

Thanks again your awesome!!!;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.