Resources » Code Snippets » VB.NET Syntax

Check whether the text entered in a text box is decimal or not


Posted Date: 30-Jan-2008  Last Updated:   Category: VB.NET Syntax    
Author: Member Level: Gold    Points: 5



The sample VB.NET code is used to check if the value in a text box is decimal or not. This code can be written in textbox keypress event



Private Sub numericTextboxKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) ' Handles TextBox1.KeyPress, TextBox2.KeyPress
Dim chr As Char = e.KeyChar
If IsNumeric(e.KeyChar) And Not e.KeyChar = "-" Then
e.Handled = Not IsNumeric(sender.Text & e.KeyChar)
ElseIf e.KeyChar = "." Then
If Not (sender.SelectedText = "." Or IsNumeric(sender.Text & e.KeyChar)) Then
e.Handled = True
End If
ElseIf e.KeyChar = "-" Then
If sender.SelectionStart <> 0 Or Microsoft.VisualBasic.Left(sender.Text, 1) = "-" Then
e.Handled = True
End If
ElseIf Not Char.IsControl(e.KeyChar) Then
e.Handled = True
End If
End Sub




Did you like this resource? Share it with your friends and show your love!

Responses to "Check whether the text entered in a text box is decimal or not"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India