vb.net 只允许 2 个小数点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6401090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Allow only 2 Decimal points
提问by soclose
In window textbox, I'd like to just allow 2 decimal pointsonly. I could set text box only numeric but don't know how to limit 2 decimal points.
在窗口文本框中,我只想允许2 个小数点。我只能将文本框设置为数字,但不知道如何限制 2 个小数点。
For example, 743.56
例如,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?
如何?
回答by icktoofay
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.
尝试使用 aNumericUpDown
而不是 a TextBox
。ANumericUpDown
允许您指定要转到的小数位数,并自动将其限制为数字。您还有两个方便的上下按钮。
回答by ThatGuyInIT
A simpler solution would be to just use:
一个更简单的解决方案是使用:
txtPrice.Text = String.Format("{0:n2}", numberVariableHere);
回答by DareDevil
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.
这是代码,我已经测试过了,我只是允许用户只输入小数点后一位;您可以将值 2 更改为您的选择。
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
我已经测试了最多 2,3,4 个小数点的代码
回答by Suji
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
回答by Bulis
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
回答by Anil Singh
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
回答by soclose
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