Validate Event on Text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Echooff3
    New Member
    • Jul 2007
    • 22

    #1

    Validate Event on Text box

    I'm ready to pull my hair out with this!!! I'm trying to validate a textbox so that only a Integer or a Decimal can be entered. If I enter a 1 in the box it validates fine and the focus moves to the next control. If I enter 1.5 it validates but the focus is locked into textbox and I can't click or tab out of the box. I tried flipping the e.cancel value but that did not work.

    Here's the code:
    Code:
     Private Sub txtInterest_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtInterest.Validating
    
            Try
                Decimal.Parse(txtInterest.Text)
                e.Cancel = False
            Catch ex As Exception
                MessageBox.Show("Please enter the interest amount in the form of a decimal.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                e.Cancel = True
            End Try
    
        End Sub
    I was having simular issues trying to use the Decimal.TryPars e()
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by Echooff3
    I'm ready to pull my hair out with this!!! I'm trying to validate a textbox so that only a Integer or a Decimal can be entered. If I enter a 1 in the box it validates fine and the focus moves to the next control. If I enter 1.5 it validates but the focus is locked into textbox and I can't click or tab out of the box. I tried flipping the e.cancel value but that did not work.

    Here's the code:
    Code:
     Private Sub txtInterest_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtInterest.Validating
    
            Try
                Decimal.Parse(txtInterest.Text)
                e.Cancel = False
            Catch ex As Exception
                MessageBox.Show("Please enter the interest amount in the form of a decimal.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                e.Cancel = True
            End Try
    
        End Sub
    I was having simular issues trying to use the Decimal.TryPars e()
    You can use some other method if this one is not working. say that
    abs(txbox1.text ) < 1 and txbox1.text <> 0 to find out if its a decimal, or
    int(txbox1.text ) = txbox1.text if its an integer... and of course the else case would be when you have an integer with a decimal.

    hope that helps

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by kadghar
      You can use some other method if this one is not working. say that abs(txbox1.text ) < 1 ... etc...
      Does VB.Net no longer have the IsNumeric() or equivalent function?

      Comment

      Working...