Newbie
Newbie

Reputation: 873

IsNumeric on VS2012 (windows forms)

I am using the following code:

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

Basically what I need is to check if the data inserted on the textbox is only numbers or not, when I insert letters or special characters like # or $ it works just fine and the error pops up, but if I enter +6 it goes through to the code.

Is this normal? If so is there a way to give the error even when it has a + or -? When I use *, / or = it also pops-up the error.

Upvotes: 0

Views: 490

Answers (5)

Don Thomas Boyle
Don Thomas Boyle

Reputation: 3045

Just incase you want to have it look a bit better try

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

PS: Glad you figured it out hope this helps

Upvotes: 1

Rose
Rose

Reputation: 641

Maybe using the KeyDown event of your text control will help?

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

http://www.asciitable.com/index/asciifull.gif

Upvotes: 0

Newbie
Newbie

Reputation: 873

Thanks for all your help, i tried you suggestions but somehow they didnt work :S

Managed to solve the problem like this:

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

Again thanks for the help

Upvotes: 0

TTT
TTT

Reputation: 1885

As I understand, you only want to keep numbers chars (no ".", ",", ...) Using Where() and VB's lamba expression. This selects then count any caracter not in the digits string, then checks if the count is equal to zero.

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

Better, using Any() instead of Where().Count()

If IsNumeric(Not myString.Any(Function(c) Not "0123456789".Contains(c))) Then
    not important code
Else
    MsgBox("O campo minutos só pode conter números!")
End If

This could probably be done with a regular expression too.

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 942020

IsNumeric() casts a pretty big net. It also considers currency values to be numbers. Well, they certainly are to an accountant.

If you want to make it more restrictive then use a conversion method that's more specific to the type of number you like. Like Double.TryParse().

Upvotes: 1

Related Questions