2014-04-08 42 views

回答

2
Dim int As Integer 
If Integer.TryParse("12345", int) Then 
    'use int variable it holds the converted value 
End If 
2

为@OneFineDay说,你必须使用Integer.TryParse,现在重复问题,直到它是一个整数,你可以这样做:

dim ans = As String 
dim int As Integer 
dim isInteger As Boolean = False 

do While Not isInteger 
    ans = InputBox("Give me an Integer") 
    isInteger = Integer.TryParse(ans, int) 
End do 

''Here int holds an integer 
+0

“12345”在圆括号中代表什么? – Questi0nM4rk

+0

抱歉应该回答 –

0

12345代表的文字,你既可以在引号手动输入,或者可以从这样的输入框抢:

Integer.TryParse(InputBox("Enter integer here"),myInt),使得从输入框的输入是将被转换成整数变量的一个,这可以节省存储器无需声明另一瓦里能够。
在@ bto.rdz的答案循环是很方便,虽然,特别是如果你希望用户无论输入整数什么

1

它不会是一个输入验证问题没有一个正则表达式的答案,因此,如果你想让它更复杂得多它需要,那么你可以使用像

Dim expression As New Regex("^-?\d+$") 
If Not expression.IsMatch(textBox1.Text) Then 
    textBox1.Text = String.Empty 
End If 

正则表达式模式将着眼于输入的所有文本,并匹配当且仅当出现零个或一个减号后面至少一个数字。