VB NET 限制文本框中的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19054275/
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
VB NET limit numeric in textbox
提问by Chozo Qhai
I had created textbox in my program. I need to ask user to input the value in textbox without using numeric control. When they exceed the boundary numeric that I set, error message will pop out immediately. How can I do that ? Another methods also will appreciated! Thanks You.
我在我的程序中创建了文本框。我需要让用户在不使用数字控件的情况下在文本框中输入值。当它们超过我设置的边界数字时,会立即弹出错误消息。我怎样才能做到这一点 ?另一种方法也将不胜感激!感谢您。
回答by Tim Schmelter
Use Int32.TryParseand then check if the value is inside the boundaries.
使用Int32.TryParse然后检查值是否在边界内。
Dim minimum = 10
Dim maximum = 100
Dim number As Int32
If Not Int32.TryParse(txtNumber.Text, number) Then
MessageBox.Show("Please enter a valid integer!")
Return
End If
If number < minimum OrElse number > maximum Then
Dim message = String.Format("Please enter an integer between {0} and {1}!",
minimum, maximum)
MessageBox.Show(message)
Return
End If
' all is ok, go on ...
回答by Alexandre
The answer given by @Tim Schmelter is correct, to get numbers on TextBox you should
@Tim Schmelter 给出的答案是正确的,要在 TextBox 上获取数字,您应该
check the DecimalUpdown class on Extend WPF Toolkit
检查扩展 WPF 工具包上的 DecimalUpdown 类
:)
:)
回答by Steve
Here is the code I have used for years for DotNet textboxes. You may need to change for your specific implementation but likely not.
这是我多年来用于 DotNet 文本框的代码。您可能需要针对您的特定实现进行更改,但可能不需要。
Put this code in a module, so you can call it from multple places:
将此代码放在一个模块中,以便您可以从多个地方调用它:
Public Sub NumericTextboxKeyPress(ByVal txt As TextBox, ByVal e As System.Windows.Forms.KeyPressEventArgs, Optional ByVal AllowNegative As Boolean = True, Optional ByVal AllowDecimal As Boolean = True)
Dim dot As Boolean = False
Dim flag As Boolean = False
If e.KeyChar = ControlChars.Back Then
Return
End If
If e.KeyChar = "."c AndAlso Not AllowDecimal Then
flag = True
End If
If e.KeyChar = "-"c AndAlso Not AllowNegative Then
flag = True
End If
If e.KeyChar = "."c And txt.Text.IndexOf("."c) > 0 Then
dot = True
End If
If e.KeyChar < "-"c Or e.KeyChar > "9"c Or dot = True Then
flag = True
End If
If txt.Text.Length > 0 AndAlso e.KeyChar = "-"c AndAlso (txt.SelectionStart > 0 OrElse txt.Text.IndexOf("-"c) >= 0) Then
flag = True
End If
If e.KeyChar = "/"c Then
flag = True
End If
If flag = True Then
e.Handled = True
End If
End Sub
Public Function ValidNumericTextboxValue(ByVal txt As TextBox, ByVal Precision As Integer, ByVal Scale As Integer, ByVal Style As String, Optional ByVal ThrowError As Boolean = True) As Boolean
If txt.Text Is Nothing OrElse txt.Text.Trim = "" Then
txt.Text = Format(0, Style)
Return True
Else
Dim Value As Object
Dim dMax As Decimal
Dim dMin As Decimal
dMax = New String("9", Precision - Scale) & "." & New String("9", Scale)
dMin = "-" & New String("9", Precision - Scale) & "." & New String("9", Scale)
If IsNumeric(txt.Text) Then
'Make sure there are not more digits after the decimal than allowed
Value = Math.Round(CDec(txt.Text), Scale)
If Value > dMax Then
If ThrowError Then
Throw New Exception("Numeric Value is geater than allowed! Max value is: " & FormatNumber(dMax, Scale, TriState.False, TriState.UseDefault, TriState.True))
Else
MessageBox.Show("Numeric Value is geater than allowed! Max value is: " & FormatNumber(dMax, Scale, TriState.False, TriState.UseDefault, TriState.True))
End If
Return False
ElseIf Value < dMin Then
If ThrowError Then
Throw New Exception("Numeric Value is less than allowed! Min value is: " & FormatNumber(dMin, Scale, TriState.False, TriState.UseDefault, TriState.True))
Else
MessageBox.Show("Numeric Value is less than allowed! Min value is: " & FormatNumber(dMin, Scale, TriState.False, TriState.UseDefault, TriState.True))
End If
Return False
End If
'If I am here, lets format it and put it back in the textbox
txt.Text = Format(Value, Style)
Return True
Else
If ThrowError Then
Throw New Exception("This must be a numeric value! Max value is: " & FormatNumber(dMax, Scale, TriState.False, TriState.UseDefault, TriState.True))
Else
MessageBox.Show("This must be a numeric value! Max value is: " & FormatNumber(dMax, Scale, TriState.False, TriState.UseDefault, TriState.True))
End If
Return False
End If
End If
End Function
Call the NumericTextboxKeyPressmethod from the keypress event of the textbox, this will prevent them from typing special characters (non-numeric).
NumericTextboxKeyPress从文本框的按键事件调用该方法,这将阻止他们输入特殊字符(非数字)。
Call the ValidNumericTextboxValuefrom the validate event of the textbox. This will format the text and validation precision and scale.
ValidNumericTextboxValue从文本框的验证事件中调用。这将格式化文本和验证精度和比例。
Enjoy
享受

