Vb.net VS2012上的IsNumeric(windows窗体)

Vb.net VS2012上的IsNumeric(windows窗体),vb.net,winforms,visual-studio,Vb.net,Winforms,Visual Studio,我正在使用以下代码: If IsNumeric(TextBox2.Text) Then not important code Else MsgBox("O campo minutos só pode conter números!") End If 基本上,我需要检查文本框中插入的数据是否仅为数字,当我插入字母或特殊字符(如#或$)时,它工作正常并弹出错误,但如果输入+6,则会进入代码 这正常吗?如果是这样的话,有没有办法给出错误,即使它有+或-?当我使用*,/或=时,它也会弹

我正在使用以下代码:

If IsNumeric(TextBox2.Text) Then
    not important code
Else
    MsgBox("O campo minutos só pode conter números!")
End If
基本上,我需要检查文本框中插入的数据是否仅为数字,当我插入字母或特殊字符(如#或$)时,它工作正常并弹出错误,但如果输入+6,则会进入代码


这正常吗?如果是这样的话,有没有办法给出错误,即使它有+或-?当我使用*,/或=时,它也会弹出错误。

据我所知,您只想保留数字字符(否“,”,…) 使用
Where()
和VB的lamba表达式。这将选择然后统计不在数字字符串中的任何字符,然后检查计数是否等于零

If IsNumeric(myString.Where(Function(c) Not "0123456789".Contains(c)).Count() = 0) Then
    not important code
Else
    MsgBox("O campo minutos só pode conter números!")
End If
最好使用
Any()
而不是
Where().Count()


这可能也可以用正则表达式来完成。

据我所知,您只希望保留数字字符(不“,”,…) 使用
Where()
和VB的lamba表达式。这将选择然后统计不在数字字符串中的任何字符,然后检查计数是否等于零

If IsNumeric(myString.Where(Function(c) Not "0123456789".Contains(c)).Count() = 0) Then
    not important code
Else
    MsgBox("O campo minutos só pode conter números!")
End If
最好使用
Any()
而不是
Where().Count()

这可能也可以通过正则表达式来实现。

IsNumeric()的作用非常大。它还将货币值视为数字。嗯,他们当然是会计

如果您想使其更具限制性,那么请使用一种转换方法,该方法针对您喜欢的数字类型更为具体。比如Double.TryParse()。

IsNumeric()会撒下一张很大的网。它还将货币值视为数字。嗯,他们当然是会计


如果您想使其更具限制性,那么请使用一种转换方法,该方法针对您喜欢的数字类型更为具体。例如Double.TryParse()。

谢谢你的帮助,我尝试了你的建议,但不知怎么的,它们都不起作用:S

设法解决了这样的问题:

For Each c As Char In TextBox1.Text
    If c = "+" Or c = "-" Then
        i = i + 1
    End If
Next
If IsNumeric(TextBox2.Text) And i = 0 Then
    not important code
Else
    MsgBox("O campo minutos só pode conter números!")
End If

再次感谢您的帮助

谢谢您的帮助,我尝试了您的建议,但不知怎的,它们都不起作用:S

设法解决了这样的问题:

For Each c As Char In TextBox1.Text
    If c = "+" Or c = "-" Then
        i = i + 1
    End If
Next
If IsNumeric(TextBox2.Text) And i = 0 Then
    not important code
Else
    MsgBox("O campo minutos só pode conter números!")
End If

再次感谢您的帮助

也许使用文本控件的KeyDown事件会有所帮助

Private Sub TextBox2_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
    Select Case e.KeyValue
        Case Keys.NumPad0 To Keys.NumPad9
            ' code here for keys 0 through 9 from Keypad
        Case Keys.D0 To Keys.D9
            ' code here for keys 0 through 9 from top of keyborad

        Case Keys.Multiply, Keys.Divide, Keys.Add
            ' code here gor other characters like * / + etc.
        Case Else
            ' Code here for all other keys, supress key if needed
            e.SuppressKeyPress = True
    End Select
End Sub

也许使用文本控件的按键事件会有所帮助

Private Sub TextBox2_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
    Select Case e.KeyValue
        Case Keys.NumPad0 To Keys.NumPad9
            ' code here for keys 0 through 9 from Keypad
        Case Keys.D0 To Keys.D9
            ' code here for keys 0 through 9 from top of keyborad

        Case Keys.Multiply, Keys.Divide, Keys.Add
            ' code here gor other characters like * / + etc.
        Case Else
            ' Code here for all other keys, supress key if needed
            e.SuppressKeyPress = True
    End Select
End Sub

如果你想让它看起来更好一点,试试看

If IsNumeric(TextBox2.Text) And TextBox2.Text.Contains("+") or Textbox2.Text.Contains("-")        
Then
    not important code
Else
MsgBox("O campo minutos só pode conter números!")
End If

附言:很高兴你找到了希望这有帮助,以防你想让它看起来更好一点试试

If IsNumeric(TextBox2.Text) And TextBox2.Text.Contains("+") or Textbox2.Text.Contains("-")        
Then
    not important code
Else
MsgBox("O campo minutos só pode conter números!")
End If

附言:很高兴你知道了希望这有帮助

一元加号和减号通常被允许用于数字。大多数语言中都没有一元
*
/
=
。你想只允许数字吗?是的,达米恩,我想只允许数字一元加号和减号通常用于数字。大多数语言中都没有一元
*
/
=
。你想只允许数字吗?是的,Damien,我想只允许数字