VB NET限制文本框中的数字

时间:2013-09-27 15:14:52

标签: vb.net textbox numeric

我在程序中创建了文本框。 我需要让用户在不使用数字控件的情况下在文本框中输入值。 当它们超出我设置的边界数字时,将立即弹出错误消息。 我怎样才能做到这一点 ? 另一种方法也将受到赞赏! 谢谢你。

3 个答案:

答案 0 :(得分: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 ...

答案 1 :(得分:1)

@Tim Schmelter给出的答案是正确的,要在TextBox上获取数字

检查 Extend WPF Toolkit

上的DecimalUpdown类

:)

答案 2 :(得分: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。这将格式化文本和验证精度和比例。

享受