怎怎么样在vb 中判断文本框中输入的是字符串还是数字,并把判断的结果用msgbox弹出窗口显示。

一点要把代码写全!
2024-12-29 04:42:37
推荐回答(4个)
回答1:

Private Sub Command1_Click()
If Text1 = "" Then MsgBox "无内容": Exit Sub
If IsNumeric(Text1.Text) Then MsgBox "是数字" Else MsgBox "不是数字"
End Sub

回答2:

Private Function Pd(str As String)
Dim lstr%, k$, l%, b%, c%
lstr = Len(str)
For i = 1 To Len(str) Step 1
k = Mid$(str, i, 1)
For j = 65 To 90
If k = Chr$(j) Then l = l + 1
Next j
For t = 97 To 122
If k = Chr$(t) Then l = l + 1
Next t
For p = 48 To 57
If k = Chr$(p) Then b = b + 1
Next p
If k = Chr$(32) Then c = c + 1
Next i
Pd = l & "个字母" & b & "个数字" & c & "个空格"
End Function

Private Sub Form_Load()
Dim x$
x = InputBox("请输入", "提示")
MsgBox Pd(x)
End Sub
我尝试做一下,不知道这方法可行不?

这个更有效率

Private Function Pd(str As String)
Dim lstr%, k$, l%, b%, c%
lstr = Len(str)
For i = 1 To Len(str) Step 1
k = Mid$(str, i, 1)
If Chr$(k) = 32 Then c = c + 1
If Chr$(k) > 64 and Chr$(j) < 90 Then l = l + 1 : goto 10
If Chr$(k) > 96 and Chr$(j) < 123 Then l = l + 1 : goto 10
If Chr$(k) > 47 and Chr$(j) < 58 Then b = b + 1 : goto 10
10 Next
Pd = l & "个字母" & b & "个数字" & c & "个空格"
End Function

Private Sub Form_Load()
Dim x$
x = InputBox("请输入", "提示")
MsgBox Pd(x)
End Sub

回答3:

有ASCII代码的
比如判断数字的
48-57是数字
65-90是大写的英文字母
97-122小写的英文字母
你判断键盘的键值,就可以知道了

回答4:

if isnumeric(text1.text) then
msgbox "文本框中的是数字"
else
msgbox "文本框中的是字符串"
end if