Oct 27, 2011

How to Validate Only Numeric Value in VB.NET TextBox?

You have to write these code on Validating event of TextBox.

Private Sub NumericValidation(ByVal sender As Object, ByVal e As CancelEventArgs) Handles textBox1
    Dim txtTemp As TextBox
    txtTemp = DirectCast(sender, TextBox) 'Explicit type casting of sender to TextBox  
    If TypeOf txtTemp Is TextBox Then
       If IsNumeric(txtTemp.Text) = False Then
          MessageBox.Show("Please enter only numeric value.")
          txtTemp.Text = String.Empty
          txtTemp.Focus()
       End If
    End If
End Sub

No comments: