locked
Decimal validation for textbox

    Question

  • I want to validate a textbox like it accept only digits and a decimal point

     

    Code Snippet

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

                           

    If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not Asc(e.KeyChar) = 46 Then

    e.Handled = True

    End If

     

    now i need to restrict the textbox from entering more than one decimal point and it should display only two digits after the decimal point.

    Saturday, April 26, 2008 9:20 AM

Answers

  • Place the following code in the textbox's KeyPress event.  It will only allow a real number to be entered, including one decimal point, as well as either a positive or negative number.  It will only allow input of up to 2 digits past the decimal point.

     

     

    Code Snippet

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim dot As Integer, ch As String

    If Not Char.IsDigit(e.KeyChar) Then e.Handled = True

    If e.KeyChar = "-" And TextBox1.SelectionStart = 0 Then e.Handled = False 'allow negative number

    If e.KeyChar = "." And TextBox1.Text.IndexOf(".") = -1 Then e.Handled = False 'allow single decimal point

    dot = TextBox1.Text.IndexOf(".")

    If dot > -1 Then 'allow only 2 decimal places

       ch = TextBox1.Text.Substring(dot + 1)

       If ch.Length > 1 Then e.Handled = True

    End If

    If e.KeyChar = Chr(13) Then TextBox2.Focus() 'Enter key moves to specified control, or:

    'If e.KeyChar = Chr(13) Then GetNextControl(TextBox1, True).Focus() 'Enter key moves to next control in Tab order

    If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace

    End Sub

     

     

     

     

    Saturday, April 26, 2008 10:51 PM

All replies

  • You could try something like the following.  If your data type is not Decimal, substitute the type you really want.  By using the correct type, this code will also prevent entry of too many digits for the data type.

     

    Code Snippet

    Private _PreviousKnownGoodText As String = ""

     

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    Dim dummy As Decimal

    If TextBox1.Text <> "" AndAlso Not Decimal.TryParse(TextBox1.Text, _

    System.Globalization.NumberStyles.AllowDecimalPoint, Nothing, dummy) Then

     

    Dim savedCursorPosition As Integer = TextBox1.SelectionStart

    TextBox1.Text = _PreviousKnownGoodText

    If savedCursorPosition >= 1 Then

    TextBox1.SelectionStart = savedCursorPosition - 1

    End If

    Else

    _PreviousKnownGoodText = TextBox1.Text

    End If

    End Sub

     

    Saturday, April 26, 2008 1:20 PM
  • Hi,

     

    Typing something like

     

    .34

     

    is also valid.

     

    Alter this line only if say you want 4 decimal places.>>

     

    If decimalPlaces > 4 Then e.Handled = True

     

    Code Snippet

     

    Option Strict On

     

    Public Class Form1

     

    Dim decimalPlaces As Integer = 0

     

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

     

    If TextBox1.Text.IndexOf(".") = 0 Then

    TextBox1.Text = "0" & TextBox1.Text

    TextBox1.SelectionStart = 2

    End If

    If TextBox1.Text.IndexOf(".") >= 0 Then

     

    decimalPlaces = TextBox1.Text.Substring(TextBox1.Text.IndexOf(".")).Length

     

    Else

    decimalPlaces = 0

    End If

     

    If decimalPlaces > 2 Then e.Handled = True

     

    If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And TextBox1.Text.IndexOf(".") >= 0 Then

     

    e.Handled = True

    'Allow the Backspace key.

    ElseIf e.KeyChar = Chr(Keys.Back) Then

    e.Handled = False

    End If

    End Sub

     

    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

     

    Dim myDecimal As Decimal

    Decimal.TryParse(TextBox1.Text, myDecimal)

    Me.Text = myDecimal.ToString

    End Sub

    End Class

     

     

     

     

    Regards,

     

    John

     

    Saturday, April 26, 2008 3:15 PM
  • Place the following code in the textbox's KeyPress event.  It will only allow a real number to be entered, including one decimal point, as well as either a positive or negative number.  It will only allow input of up to 2 digits past the decimal point.

     

     

    Code Snippet

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim dot As Integer, ch As String

    If Not Char.IsDigit(e.KeyChar) Then e.Handled = True

    If e.KeyChar = "-" And TextBox1.SelectionStart = 0 Then e.Handled = False 'allow negative number

    If e.KeyChar = "." And TextBox1.Text.IndexOf(".") = -1 Then e.Handled = False 'allow single decimal point

    dot = TextBox1.Text.IndexOf(".")

    If dot > -1 Then 'allow only 2 decimal places

       ch = TextBox1.Text.Substring(dot + 1)

       If ch.Length > 1 Then e.Handled = True

    End If

    If e.KeyChar = Chr(13) Then TextBox2.Focus() 'Enter key moves to specified control, or:

    'If e.KeyChar = Chr(13) Then GetNextControl(TextBox1, True).Focus() 'Enter key moves to next control in Tab order

    If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace

    End Sub

     

     

     

     

    Saturday, April 26, 2008 10:51 PM
  • One needs to be careful with the KeyPress solutions to this problem.  The user can still right-click and use the Paste command to paste text that does not conform to the validation.

     

    A common solution is to have both a KeyPress and a TextChanged handler.

     

    Saturday, April 26, 2008 10:54 PM
  • I added code to my above post to allow input of only 2 decimal places.  I also moved the line that allows the backspace key to the line before last, otherwise you can't backspace after attempting to enter a 3rd decimal place.  However, you  can't enter anything else after that unless you delete everything.

     

    Saturday, April 26, 2008 11:17 PM
  • You will get the idea, how to restrict pasting into a textbox from this site

     

    Windows Forms FAQ - Windows Forms TextBox.htm

     

    Monday, April 28, 2008 4:04 AM