Hi all,
I have different text boxes the I need to validate the user inut. The code that I have (example) does work, the problem is that if there is absolutly nothing in the text box I don't want to get the error message. If I have a starting "0", physically in the text box, or "non-numeric value" then I do want the error message. What happens is when I "clear" everything in the boxes then I get the error also but don't want it if nothing is in the boxes.

Private Sub txtInterestRate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInterestRate.TextChanged
        Try

            If Me.txtInterestRate.Text.Length = 0 Then          'reads interest rate text box
                Me.errInput.SetError(Me.txtInterestRate, "Please Enter a valid value") 'if 0 gives error

            ElseIf Not IsNumeric(txtInterestRate.Text) Then     'checks input for numeric value
                Me.errInput.SetError(Me.txtInterestRate, "Please Enter a valid Numeric value") 'if non-numerics gives error
                Throw New Exception("Please enter a valid numeric value")

            ElseIf txtInterestRate.Text = 0 Then                'checks input for numeric value
                Me.errInput.SetError(Me.txtLoanAmount, "Please Enter a Non Zero valid Numeric value") 'if non-numerics gives error
                Throw New Exception("Please enter a valid Non Zero numeric value")

            End If

        Catch ex As Exception       'exception handeler for error input
            MsgBox(ex.Message)      'displays error
            Me.txtInterestRate.Text = ("")
        End Try
    End Sub

Any help would really be appreciated!
Thanks,
Ken

What about right after Try inserting

If Me.txtInterestRate.Text <> ""
/// do all of your stuff
End If

rapture,
Thank you very much for your reply. I added your line of code to all the text boxes and it worked great.
Thanks,
Ken

Hi All,

I have been trying to figure out another problem with this program, maybe some one can help me out.
I need some type of code so if the text field is left completey empty it will give an error and tell the user to please input a number.
I just haven't been able to figure it out.

Help Please,
Ken

You may want to check that when user presses Ok-button (or any similar button):

Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
  ' Check empty text

  If Me.txtInterestRate.Text.Trim = "" Then
    MessageBox.Show("Please enter interest rate", _
      "Missing Information", _
      MessageBoxButtons.OK, _
      MessageBoxIcon.Warning)
    Me.txtInterestRate.Focus() ' Set focus to field if you want
  End If

End Sub

or "integrate" this with rapture's answer:

If Me.txtInterestRate.Text <> ""
  /// do all of your stuff
Else
  MessageBox.Show("Please enter interest rate", _
     "Missing Information", _
     MessageBoxButtons.OK, _
     MessageBoxIcon.Warning)
End If

Teme64,
Thank you for your input, it works great now. I just couldn't figure that one out.
Thanks,
tyserman5674

This article has been dead for over six months. Start a new discussion instead.