In window textbox, I'd like to just allow 2 decimal points only. I could set text box only numeric but don't know how to limit 开发者_StackOverflow中文版2 decimal points.
For example, 743.56
My code is below
Private Sub txtPrice_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPrice.KeyPress
'allow numeric
If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "."c Then
e.Handled = True
End If
' only allow one decimal point
If e.KeyChar = "."c AndAlso TryCast(sender, TextBox).Text.IndexOf("."c) > -1 Then
e.Handled = True
End If
End Sub
How to?
Try using a NumericUpDown
rather than a TextBox
. A NumericUpDown
lets you specify the number of decimal places to go to, as well as automatically limiting it to numbers. You also have two handy little up and down buttons.
A simpler solution would be to just use:
txtPrice.Text = String.Format("{0:n2}", numberVariableHere);
Here is the code, I have tested, I am just giving permission to user to enter only one digit after decimal point; you can change the value 2 to up to your choice.
Private Sub tb_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tb.KeyPress
If Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = "." Then
e.Handled = True
Else
If e.KeyChar = "." And tb.Text.IndexOf(".") <> -1 Then
e.Handled = True
ElseIf e.KeyChar = "." Then
e.Handled = False
ElseIf Char.IsDigit(e.KeyChar) Then
If tb.Text.IndexOf(".") <> -1 Then
If tb.Text.Length >= tb.Text.IndexOf(".") + 2 Then 'replace 2 for greater numbers after decimal point
e.Handled = True
End If
End If
End If
End If
End Sub
I have tested this code for up-to 2,3,4 decimal points
Now it works. Please check my code below
'2 decimal points only
'If key-in is after decimal point
If txtPrice.SelectionStart > txtPrice.Text.IndexOf(Chr(46)) Then
'If text not select All
If txtPrice.SelectedText.Length = 0 Then
If (txtPrice.Text.Trim() <> "") Then
If (rexPrice.IsMatch(txtPrice.Text) = False) AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End If
End If
End If
Try this code (It will allow only numbers with a decimal point):
Private Sub txtCostPrice_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCostPrice.KeyPress
If InStr(txtCostPrice.Text, ".") And e.KeyChar = "." Then e.Handled = True
If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
If e.KeyChar <> "." Then e.Handled = True
End If
End Sub
i think this code help you to solve your problem
dim n as integer
n=0
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(46) Then
n = Len(TextBox1.Text)
End If
If Len(TextBox1.Text) >= n + 2 And n <> 0 Then
TextBox1.Enabled = False
End If
End Sub
Private Sub txtVatRate_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtVatRate.KeyPress
OnlyAllowPostiveNumbers(sender, e, 1)
End Sub
Public Function OnlyAllowPostiveNumbers(sender As Object, e As System.Windows.Forms.KeyPressEventArgs, Optional ByRef iDeimalPlaces As Integer = 0) As System.Windows.Forms.KeyPressEventArgs
'Only allow numeric values with the exception of spaces ie '07969 860053' and backspace
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
e.Handled = True
End If
'Backspace
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 46) And InStr(sender.text, ".") < 1 Then
e.Handled = False
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) > 48) And (Microsoft.VisualBasic.Asc(e.KeyChar) < 57) Then
If InStr(sender.text, ".") > 0 Then
Dim n As Integer = InStr(sender.text, ".")
If n <= (sender.text.length - iDeimalPlaces) Then
e.Handled = True
End If
End If
End If
Return e
End Function
精彩评论