vb if 数据不是某种类型 then?

来源:百度知道 编辑:UC知道 时间:2024/06/15 19:05:18
如题,如何办到?
我的目的是在文本框的_chang事件中
若text1.text的数据不是数值型则msgbox

如果你想实现只输入字符的话
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub
Dim s As String
s = "1234567890"
Dim j As Integer
j = InStr(s, Chr(KeyAscii))

If j = 0 Then
KeyAscii = 0
End If
End Sub
其实有些第三方控件可以直接 text1.type= long
不过VB这里不自带。

Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
MsgBox "非数字请输入数字",,"提示"
Exit Sub
End If
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
MsgBox "不是数值"
Text1.Text = ""
End If
End Sub

用正则表达式

if Regex.IsMatch( text1.text.trim(),"表达式要求")then
mesbox“输入不符合要求”
end if

只能输入数字:"^[0-9]*$"。
只能输入n位的数字:"^\d{n}$"。
只能输入至少n位的数字:"^\d{n,}$"。
只能输入m~n位的数字:。"^\d{m