需要一个vb函数或方法!!!

来源:百度知道 编辑:UC知道 时间:2024/06/30 23:51:56
vb编程,需要一个判断text中输入的内容是字符还是数字的函数或方法,判断结果不同,执行不同的过程,谁知到,谢谢

if IsNumeric(text) then
'执行数字结果的代码
else
'执行字母结果的代码
endif

在 Text1 输入,在 Label1 显示是字符还是数字

Private Sub Text1_Change()
Label1.Caption = NumOrStr(Text1.Text)
End Sub

Private Function NumOrStr(nTxt As String) As String
Dim S As Long, IsNum As Boolean, IsStr As Boolean

If nTxt = "" Then NumOrStr = "字符(空)": Exit Function

IsNum = False: IsStr = False
For I = 1 To Len(nTxt)
S = Asc(Mid(nTxt, I, 1))
If S < Asc("0") Or S > Asc("9") Then IsStr = True Else IsNum = True
Next

If IsNum And IsStr Then
NumOrStr = "字符和数字"
Else
If IsNum Then NumOrStr = "全是数字" Else NumOrStr = "全是字符"
End If
End Function

用IsNumeric(text)就行了,True表示是数字,False表示不是数字。