为什么这个IsNum函数错了?

时间:2015-10-31 01:41:52

标签: vb.net

    Private Function IsNum(textBox As TextBox, name As String) As Boolean
    If IsNumeric(textBox) = False Then
        MessageBox.Show(name & " is not a number.", "Entry Error")
        textBox.Select()
        Return False
    Else
        Return True
    End If
End Function

写完这个功能之后,显然我是以某种方式写的,如果我做了一个数字,它并不认为它是一个数字。是否有更好的方法来编写它以使它不那么令人困惑和不正确?

3 个答案:

答案 0 :(得分:3)

由于您使用.NET Framework(基于标记vb.net) - 我建议使用强类型文本解析数字

我假设您知道您期望的数字类型(Int32DecimalDouble等。)

使用TryParse方法Int32.TryParse Method

Private Function IsNum(textBox As TextBox, name As String) As Boolean
    Dim temp As Integer
    If Integer.TryParse(textBox.Text, temp) = False Then
        MessageBox.Show(name & " is not a number.", "Entry Error")
        textBox.Select()
        Return False
    End If
    'If values is valid you can use valid Integer for your purposes
    Me.SomeInteger = temp
    Return True
End Function

答案 1 :(得分:2)

您需要检查文本框中的文本。不是文本框本身。

这样做..

 If IsNumeric(textBox.Text) = False Then

答案 2 :(得分:1)

有几种方法可以解决这个问题。首先是使用NumericUpDown控件或仅接受数字输入的自定义TextBox,如下所示,其中还考虑了从Windows剪贴板粘贴的数据。将类添加到项目中,构建,在IDE工具箱的顶部单击自定义文本框以添加到表单中,然后只需使用它。

Public Class numericTextbox
    Inherits TextBox

    Const WM_PASTE As Integer = &H302

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Dim Value As String = Me.Text
        Value = Value.Remove(Me.SelectionStart, Me.SelectionLength)
        Value = Value.Insert(Me.SelectionStart, e.KeyChar)
        e.Handled = CBool(Value.LastIndexOf("-") > 0) _
           Or Not (Char.IsControl(e.KeyChar) OrElse _
           Char.IsDigit(e.KeyChar) OrElse _
           (e.KeyChar = "."c And Not Me.Text.Contains(".") Or _
           e.KeyChar = "."c And _
           Me.SelectedText.Contains(".")) OrElse (e.KeyChar = "-"c And Me.SelectionStart = 0))
        MyBase.OnKeyPress(e)
    End Sub
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_PASTE Then
            Dim Value As String = Me.Text
            Value = Value.Remove(Me.SelectionStart, Me.SelectionLength)
            Value = Value.Insert(Me.SelectionStart, Clipboard.GetText)
            Dim result As Decimal = 0
            If Not Decimal.TryParse(Value, result) Then
                Return
            End If
        End If
        MyBase.WndProc(m)
    End Sub
End Class

依赖于目标框架3.5及更高版本的另一个想法是使用扩展方法。

Public Module LanguageExtensions
    ''' <summary>
    ''' Check if Text property of a TextBox is numeric
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.Diagnostics.DebuggerStepThrough()> _
    <System.Runtime.CompilerServices.Extension()> _
    Public Function IsNumeric(ByVal sender As TextBox) As Boolean
        Return Integer.TryParse(sender.Text, Nothing) OrElse Double.TryParse(sender.Text, Nothing)
    End Function
    ''' <summary>
    ''' Check if Text Property of a TextBox is a decimal with formatting or not
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.Diagnostics.DebuggerStepThrough()> _
    <System.Runtime.CompilerServices.Extension()> _
    Public Function IsNumericWithCurrency(ByVal sender As TextBox) As Boolean
        Return Decimal.TryParse(sender.Text, Globalization.NumberStyles.Currency, Nothing, Nothing)
    End Function

End Module

扩展方法的示例

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.IsNumericWithCurrency Then
            MessageBox.Show("Yes")
        Else
            MessageBox.Show("No")
        End If
    End Sub
End Class

如果这些都不符合您的需求,我会选择Fabio的建议。