VB中的inputbox中的文本如果只限定输入数字,要怎么写代码呀?各位帮帮我吧

[复制链接]
查看11 | 回复1 | 2007-4-12 09:53:44 | 显示全部楼层 |阅读模式
用ASC码判断输入的是不是数字
回复

使用道具 举报

千问 | 2007-4-12 09:53:44 | 显示全部楼层
简单点的:mm = InputBox("请输入数量:", "请输入")
If IsNumeric(mm)
True Then
mx = MsgBox("必须是数字!)
Endif 或者用ASCII判断If KeyAscii57 Then KeyAscii = 0 End If End Sub -----------------------------完整点的,(包括小数点,分数等等):Private Sub text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("-") '允许负数
If text1.SelStart = 0 Then
If Left(text1.Text, 1) = "-" Then
KeyAscii = 0

End If
Else
KeyAscii = 0
'
Beep
End If
Case 8
'无变化,退格键不屏蔽
Case Asc(" ") '32
If text1.SelLength = 0 Then
KeyAscii = 0
End If
Case Asc(".") '46 '允许小数点
If InStr(text1.Text, ".") Then
KeyAscii = 0
End If
Case Is
Asc(9) '57
KeyAscii = 0
MsgBox "必须是数字"
End Select
End Sub
--------------------------用ASC代码判断的:KeyPreview=True
Private Sub Form_KeyPress(KeyAscii As Integer)
KeyAscii = FilterForStr(KeyAscii)
End Sub
Public Function FilterForStr(keyvalue) As Long
FilterForStr = keyvalue
If (((keyvalue = 39) Or (keyvalue = 59))) Then
FilterForStr = 0
MSGBOX"请输入数字0~9"
END IF
End Function ---------------------------用error判断的:
Private Sub Text1_Change()
On Error GoTo ErrLine
If Text1 = "" Or Text1 = "-" Then Exit Sub
Dim x As Single
x = CSng(Text1.Text)
Exit Sub
ErrLine:
MsgBox "要求数字"
Text1.Text = Left$(Text1, Len(Text1) - 1)
Text1.SelStart = Len(Text1)
End Sub ----后三个对用的是textbox,供参考,inputbox一样可以使用讲信用,别不选答案就跑....
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行