VB中的inputbox中的文本如果只限定输入数字,要怎么写代码呀?各位帮帮我吧
2个回答
展开全部
简单点的:
mm = InputBox("请输入数量:", "请输入")
If IsNumeric(mm) <> True Then
mx = MsgBox("必须是数字!)
Endif
或者用ASCII判断
If KeyAscii < 48 Or KeyAscii > 57 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(0) '48
KeyAscii = 0
MsgBox "必须是数字"
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一样可以使用
讲信用,别不选答案就跑....
mm = InputBox("请输入数量:", "请输入")
If IsNumeric(mm) <> True Then
mx = MsgBox("必须是数字!)
Endif
或者用ASCII判断
If KeyAscii < 48 Or KeyAscii > 57 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(0) '48
KeyAscii = 0
MsgBox "必须是数字"
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一样可以使用
讲信用,别不选答案就跑....
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询