我在我的程序中创建了文本框。我需要要求用户在不使用数字控件的情况下在文本框中输入值。当它们超过我设置的边界数字时,会立即弹出错误消息。我怎样才能做到这一点 ?另一种方法也将不胜感激!感谢您。
问问题
6661 次
3 回答
3
使用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 ...
于 2013-09-27T15:20:13.987 回答
1
The answer given by @Tim Schmelter is correct, to get numbers on TextBox you should
check the DecimalUpdown class on Extend WPF Toolkit
:)
于 2013-09-27T16:06:27.597 回答
1
这是我多年来用于 DotNet 文本框的代码。您可能需要针对您的特定实施进行更改,但可能不需要。
将此代码放在一个模块中,以便您可以从多个地方调用它:
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
从文本框的按键事件调用该NumericTextboxKeyPress
方法,这将阻止他们输入特殊字符(非数字)。
从文本框的 validate 事件中调用ValidNumericTextboxValue
。这将格式化文本和验证精度和比例。
享受
于 2013-09-27T17:32:52.077 回答