检查VB2010中文本框中的输入是否为整数

时间:2018-05-21 03:49:25

标签: vb.net validation integer

如果用户输入的不是整数,程序将不会继续,但会给出一个消息框,告知错误。

这是我现在所拥有的,但它不起作用:

   Sub Validation0()
            If IsNumeric(curBat) Then
                ' Here, it still could be an integer or a floating point number
                If CLng(curBat) = curBat Then

                Else
                    MessageBox.Show("Please enter a number greater than 0.", "Error",
 MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                End If
            End If

        End Sub

1 个答案:

答案 0 :(得分:0)

尝试使代码多样化

1)确定字符串是否为整数

2)然后确定它是否大于0

  Sub Validation0()
        If IsNumeric(curBat) Then
            ' Here, it still could be an integer or a floating point number          
            dim curBat2 as integer = Convert.ToInt32(curBat)
            If curBat2 >  0 Then

            Else
                MessageBox.Show("Please enter a number greater than 0.", "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation, 
                MessageBoxDefaultButton.Button1)
            End If
        End If

    End Sub

如果要舍入整数值,请使用

     Math.Roundoff(curBat2)