Decimal validation for textbox
-
Saturday, April 26, 2008 9:20 AM
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
True End Ife.Handled =
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.
All Replies
-
Saturday, April 26, 2008 1:20 PM
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 SnippetPrivate _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, _
Nothing, dummy) ThenSystem.Globalization.NumberStyles.AllowDecimalPoint,
Dim savedCursorPosition As Integer = TextBox1.SelectionStartTextBox1.Text = _PreviousKnownGoodText
If savedCursorPosition >= 1 Then
End If ElseTextBox1.SelectionStart = savedCursorPosition - 1
End If End Sub_PreviousKnownGoodText = TextBox1.Text
-
Saturday, April 26, 2008 3:15 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 = TrueCode SnippetOption
Strict OnPublic Class Form1
Dim
decimalPlaces As Integer = 0Private
Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPressIf
TextBox1.Text.IndexOf(".") = 0 ThenTextBox1.Text =
"0" & TextBox1.TextTextBox1.SelectionStart = 2
End
IfIf
TextBox1.Text.IndexOf(".") >= 0 ThendecimalPlaces = TextBox1.Text.Substring(TextBox1.Text.IndexOf(
".")).LengthElse
decimalPlaces = 0
End
IfIf 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) Thene.Handled =
FalseEnd
IfEnd
SubPrivate Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim
myDecimal As DecimalDecimal
.TryParse(TextBox1.Text, myDecimal)Me
.Text = myDecimal.ToStringEnd
SubEnd
ClassRegards,
John
-
Saturday, April 26, 2008 10:51 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 SnippetPrivate 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 pointdot = TextBox1.Text.IndexOf(
".") If dot > -1 Then 'allow only 2 decimal placesch = 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 orderIf e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
End Sub
-
Saturday, April 26, 2008 10:54 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 11:17 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.
-
Monday, April 28, 2008 4:04 AM
You will get the idea, how to restrict pasting into a textbox from this site
Windows Forms FAQ - Windows Forms TextBox.htm

