Message Box Always Pops Up

337

New member
Joined
Sep 16, 2006
Messages
1
Programming Experience
Beginner
Hello Everyone,

In my project I added a catch exception message box for the quantityTextBox if someone entered letters or left it blank vs entering numbers.
So when I run it always pops up when I hit the calculate button. Then when I run the program at the bottom of the screem it says "A first chance exception of type 'System.FormatException' occurred in mscorlib.dll"

I have been trying to get this to work for the past 3 hours and I can't figure it out. Can anyone help me out?? Thanks in advance!
Lee:confused::confused::(
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
well what's being incorrectly formatted?

also instead of making the user enter only numbers into a textbox, why not just use the NumericUpDown control? it's made for numeric input only, including only 1 decimal point too
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
If you are going to use a TextBox for numeric input without validating the input as it's made you should handle the Validating event because that's what it's for: validating.
VB.NET:
Private Sub TextBox1_Validating(ByVal sender As Object, _
                                ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    Dim value As Double

    If Not Double.TryParse(Me.TextBox1.Text, value) Then
        MessageBox.Show("Please enter a numeric value.", _
                        "Invalid Input", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        e.Cancel = True
    End If
End Sub
That will prevent the user leaving the TextBox until they've put in a valid value.
 
Top Bottom