Results 1 to 6 of 6

Thread: Message Box error working for one type of error but not all

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2015
    Posts
    6

    Message Box error working for one type of error but not all

    Good afternoon folks,

    I stumbled on this forum during class and thought this would be the best place to get advice (my instructor was very vague and I didn't understand my questions).

    The message box says that, "Number cannot be a letter and must be positive".

    I have no issue with a negative number setting off the message box, but whenever I put a letter in the text box, no message pops up. I'll post my whole code for situation awareness, but the code that I'm having trouble will be at the bottom.

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btbcalculate.Click
         ' Declare variables.
            Dim intRunner1time As Integer
            Dim intRunner2time As Integer
            Dim intRunner3time As Integer
            Dim strRunner1 As String = String.Empty
            Dim strRunner2 As String = String.Empty
            Dim strRunner3 As String = String.Empty
    
            ' Get input 
            Try
                intRunner1time = CInt(txttime1.Text)
                intRunner2time = CInt(txttime2.Text)
                intRunner3time = CInt(txttime3.Text)
                strRunner1 = txtRunner1.Text
                strRunner2 = txtRunner2.Text
                strRunner3 = txtRunner3.Text
    
    
                    'Display race results
                    If intRunner1time < intRunner2time And intRunner1time < intRunner3time And intRunner2time < intRunner3time Then
                        lblfirst.Text = strRunner1
                        lblsecond.Text = strRunner2
                        lblthird.Text = strRunner3
                    ElseIf intRunner1time < intRunner2time And intRunner1time < intRunner3time And intRunner3time < intRunner2time Then
                        lblfirst.Text = strRunner1
                        lblsecond.Text = strRunner3
                        lblthird.Text = strRunner2
                    ElseIf intRunner2time < intRunner1time And intRunner2time < intRunner3time And intRunner1time < intRunner3time Then
                        lblfirst.Text = strRunner2
                        lblsecond.Text = strRunner1
                        lblthird.Text = strRunner3
                    ElseIf intRunner2time < intRunner1time And intRunner2time < intRunner3time And intRunner3time < intRunner1time Then
                        lblfirst.Text = strRunner2
                        lblsecond.Text = strRunner3
                        lblthird.Text = strRunner1
                    ElseIf intRunner3time < intRunner1time And intRunner3time < intRunner2time And intRunner1time < intRunner2time Then
                        lblfirst.Text = strRunner3
                        lblsecond.Text = strRunner1
                        lblthird.Text = strRunner2
                    ElseIf intRunner3time < intRunner1time And intRunner3time < intRunner2time And intRunner2time < intRunner1time Then
                        lblfirst.Text = strRunner3
                        lblsecond.Text = strRunner2
                    lblthird.Text = strRunner1
                End If
    
                'display name errors
                If txtrunner1.Text = String.Empty Then
                    MessageBox.Show("Please enter a name")
                ElseIf txtrunner2.Text = String.Empty Then
                    MessageBox.Show("Please enter a name")
                ElseIf txtrunner3.Text = String.Empty Then
                    MessageBox.Show("Please enter a name")
                End If
    
                'Display negative number and letter error
                If Integer.TryParse(txttime1.Text, intRunner1time) And IsNumeric(intRunner1time) And intRunner1time < 0 Then
                    MessageBox.Show("Number cannot be a letter and must be positve")
                ElseIf Integer.TryParse(txttime2.Text, intRunner2time) And IsNumeric(intRunner2time) And intRunner2time < 0 Then
                    MessageBox.Show("Number cannot be a letter and must be positve")
                ElseIf Integer.TryParse(txttime3.Text, intRunner3time) And IsNumeric(intRunner3time) And intRunner3time < 0 Then
                    MessageBox.Show("Number cannot be a letter and must be positve")
                End If
            Catch ex As Exception
            End Try
        End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Message Box error working for one type of error but not all

    Code:
    Try
    '...
    'your code
    Catch ex As Exception
        Msgbox(ex.message)
    End Try

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Message Box error working for one type of error but not all

    Your code doesn't work because that error ceases execution of the Try part and jumps to the Catch part... -1,-2,etc are valid integers

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Message Box error working for one type of error but not all

    It feels like maybe you saw several examples, then glued them together. You do a lot of the same work different ways within the same method.

    The problem relevant to the question, as .paul. indicated, is that CInt() throws an exception when it fails to convert its argument to an Integer. This little block of code is evil as-written:
    Code:
    Catch ex As Exception
    End Try
    What that says is, "If any error happens at all, ignore it and pretend everything is fine." That's not usually what you want. You usually want to at least let yourelf know that an error happened. At the very least, you should modify it to this:
    Code:
    Catch ex As Exception
        Console.WriteLine(ex)
    End Try
    That way you get some indication that something's gone wrong. But overall, many developers consider these catch-all blocks to be bad practice. Things work better when you can handle specific exceptions and do specific things in response. (Third-party APIs sometimes make this too difficult.)

    Anyway, since you know your input can be invalid, it's not really safe to use CInt() on your strings. You could handle the exception, but usually people prefer to use other control flow. See further down where you use Integer.TryParse()? You should use it instead of CInt() at the top. In fact, that code sholud probably just be moved to the top. It looks like this right now:
    Code:
    Try
      ' Assume the input is good and do the math
      ' If the input was bad, print an error
    Catch
      ' If an error happens, pretend it didnt.
    End Catch
    This is sort of like describing the process of backing out of a parking space as, "Put the car in reverse, accelerate, then look behind you to make sure nobody is standing behind you." That's a recipe for an accident. You want, "Look behind you, check for pedestrians, then put the car in reverse and accelerate.":
    Code:
    ' TryParse() an integer, if it's invalid:
        ' Display an error message and stop.
    ' TryParse() the next integer...
    '
    ' Do the math on all the integers, if we get here they are all valid.
    You don't really need the Try..Catch if you reorganize that way.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2015
    Posts
    6

    Re: Message Box error working for one type of error but not all

    Thanks a lot everyone, sorry for very late reply, but your advice helped out greatly!

  6. #6
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Message Box error working for one type of error but not all

    An example of getting an Integer from a TextBox without needing to use Try/Catch.

    All in one go, showing only a single error message:
    vb.net Code:
    1. Dim Runner1TimeInteger As Integer
    2. Dim Runner1timeString As String = txttime1.Text.Trim '<- Time1TextBox would be easier name to read
    3.  
    4. If Runner1timeString.Length > 0 AndAlso Integer.TryParse(Runner1timeString, Runner1TimeInteger) AndAlso Runner1TimeInteger > 0 Then
    5.     'You have valid input that's an integer that's positive
    6. Else
    7.     MessageBox.Show("Please input a positive integer value", "Bad Input", MessageBoxButtons.OK, MessageBoxIcon.Error)
    8.     txttime1.Focus()
    9. End If
    If you need error messages broken out you can do this:
    vb.net Code:
    1. Dim Runner1TimeInteger As Integer
    2. Dim Runner1timeString As String = txttime1.Text.Trim '<- Time1TextBox would be easier name to read
    3.  
    4. If Runner1timeString.Length > 0 Then
    5.     If Integer.TryParse(Runner1timeString, Runner1TimeInteger) Then
    6.         If Runner1TimeInteger > 0 Then
    7.             'You have valid input that's an integer that's positive
    8.         Else
    9.             MessageBox.Show("Please provide a positive integer", "Bad Input", MessageBoxButtons.OK, MessageBoxIcon.Error)
    10.             txttime1.Focus()
    11.         End If
    12.     Else
    13.         MessageBox.Show("Please integer input", "Bad Input", MessageBoxButtons.OK, MessageBoxIcon.Error)
    14.         txttime1.Focus()
    15.     End If
    16. Else
    17.     MessageBox.Show("Please provide input", "Input Missing", MessageBoxButtons.OK, MessageBoxIcon.Error)
    18.     txttime1.Focus()
    19. End If
    If you have a lot of these textboxes I would recommend making a function to get the input, have it accept the TextBox as a parameter and handle the showing of the error message(s) and it would simply return the integer value (or 0 when bad input given).
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width