title: How can I essentially stop my VB program until a valid value is entered via Text Box?
body: <p>How can I work around an invalid/null value entered into a text box?</p>
  
  <p>Basically I have a simple program I need to develop to solve a simple quadratic equation:</p>
  
  <pre><code>Public Class Main
  
      Private Sub btnHelp_Click(sender As System.Object, e As System.EventArgs) Handles btnHelp.Click
          UserGD.Show()
      End Sub
  
      Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
          txtStepX.Text = ""
          lstResult.Items.Clear()
      End Sub
  
      Private Sub cmdGo_Click(sender As System.Object, e As System.EventArgs) Handles cmdGo.Click
          Do
              txtStepX.Text = InputBox("Please enter a valid value for x!", "Oops!", "")
          Loop While String.IsNullOrEmpty(txtStepX.Text)
  
              Dim stepx As Single
              Dim X As Single
              Dim y As Single
              stepx = txtStepX.Text
              Dim result As String
  
              For X = -5 To 5 Step stepx
                  y = (3 * (X) ^ 2) + 4
                  result = ("x = " &amp; X &amp; " &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; y = " &amp; y)
                  lstResult.Items.Add(result)
              Next
      End Sub
  End Class
  </code></pre>
  
  <p>This is the part I want to focus on:</p>
  
  <pre><code>    Do
              txtStepX.Text = InputBox("Please enter a valid value for x!", "Oops!", "")
      Loop While String.IsNullOrEmpty(txtStepX.Text)
  
              Dim stepx As Single
              Dim X As Single
              Dim y As Single
              stepx = txtStepX.Text
              Dim result As String
  </code></pre>
  
  <p>The above manages to check the text box (after its text value is changed by the Input Box) and loops fine, as long as it is empty... However, say I put a letter in, say, "l", it crashes and Visual Studio gives me the error "Conversion from string 'l' to type 'Single' is not valid." - which is fair enough. Although, I assumed the Null in <code>isNullorEmpty</code> meant it would catch invalid values, such as strings when it's supposed to be single.</p>
  
  <p>I've tried quite a few things, and even my Computing teacher has been trying to get a solution.</p>
  
  <p>I can't remember most of the things I've tried thus far so if I'll give whatever you guys come up with a go just to be on the safe side!</p>
  
  <p><strong>Update</strong>
  Although not the clearest of code - here is what I found works the best for my needs:</p>
  
  <pre><code>    'Written by a very tired and frustrated Conner Whiteside
      'With his own blood.
      'And sweat.
      'And tears.
      'Lots of tears.
      '28/10/2014
  
  Public Class Main
  
  Private Sub btnHelp_Click(sender As System.Object, e As System.EventArgs) Handles btnHelp.Click
      UserGD.Show()
  End Sub
  
  Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
      txtStepX.Text = ""
      lstResult.Items.Clear()
  End Sub
  ' On Go button click
  Private Sub cmdGo_Click(sender As System.Object, e As System.EventArgs) Handles cmdGo.Click
      'Checks if Text Box value is a number
      If IsNumeric(txtStepX.Text) Then
          'Declares variables if Text Box value is in fact a number
          Dim result As String
          Dim stepx As Double
          Dim X As Double
          Dim y As Double
          'Displays error message with a re-input opportunity if previously entered number is out of range.
          Do While txtStepX.Text &lt;= 0 Or txtStepX.Text &gt; 10
              'InputBox simply changed the value of the TextBox's Text. MUST be an InputBox or there will be nothing to break the loop.
              txtStepX.Text = InputBox("The step size for the x values must not be a negative number or above 10." &amp; vbCrLf &amp; "Please enter another number for the step size of the x values.", "Oops!")
              'Covers an Empty input given that IsNumeric does not. This can also prevent crashing if the 'Cancel' or 'X' is pressed on InputBox.
              If txtStepX.Text = "" Then
                  'Exits Sub Class in order to avoid running mathematical operations (below) with a string "". Effectively allows a 'reset'.
                  Exit Sub
              End If
          Loop
          'After all checks, sets Text Boxvalue to variable stepx
          stepx = txtStepX.Text
          'Loops the solving of the equation from x = -5 to x = 5 with the specified step size.
          For X = -5 To 5 Step stepx
              'sets the answer of the equation (y) to variable y.
              y = (3 * (X) ^ 2) + 4
              'concatenates a string in the required format.
              result = ("x = " &amp; X &amp; " &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; y = " &amp; y)
              'Adds the result to the List Box before repeating the process for the next value of x.
              lstResult.Items.Add(result)
          Next
          'Catches any non numeric inputs in the Text Box or the Input Box e.g "l".
      ElseIf Not IsNumeric(txtStepX.Text) Then
          'Displays error message before ending If statement and Sub Class, allowing the user to return to the start and try another input. - No Input Box or Exit Sub needed.
          MsgBox("Please enter a valid number for the step size of the values of x.", 64, "Oops!")
      End If
  End Sub
  End Class
  </code></pre>
  
  <p>Honestly wouldn't have gotten anywhere if it weren't for your efforts!</p>