初学者,问个很简单的问题
就是想实现在一个TextBox中输入数字,单击按钮的时候能进行检查,如果输入的是字母什么的,就提示错误,并要求重新输入。
比如一个简单的加法计算器,在两个TextBox中分别输入数字,单击按钮先进行检查,看输入的是否是数字,是数字就得出相加的结果,是字符就提示错误并要求重输。
该怎么实现?
问题点数:20、回复次数:6Top
1 楼ljhdi( )回复于 2005-06-02 18:26:52 得分 8
'文本框只能输入数字, 来自csdn
Public Sub InputNumeric(KeyAscii As Integer, txtItem As TextBox)
Select Case KeyAscii
Case Asc("-") '允许负数
If txtItem.SelStart = 0 Then
If Left(txtItem.Text, 1) = "-" Then
KeyAscii = 0
Beep
End If
Else
KeyAscii = 0
Beep
End If
Case 8
'无变化,退格键不屏蔽
Case Asc(" ") '32
If txtItem.SelLength = 0 Then
KeyAscii = 0
End If
Case Asc(".") '46 '允许小数点
If InStr(txtItem.Text, ".") Then
KeyAscii = 0
End If
Case Is < Asc(0) '48
KeyAscii = 0
Case Is > Asc(9) '57
KeyAscii = 0
End Select
End SubTop
2 楼ttksy(ziffish)回复于 2005-06-02 18:36:55 得分 0
不太懂,具体怎么弄?
我是想在输入完后单击按钮的时候检查Top
3 楼ljhdi( )回复于 2005-06-02 18:47:28 得分 0
这是写在模块里的一个标准函数
调用时这样调用
使用text的KeyPress的事件
Private Sub Text1_KeyPress(KeyAscii As Integer)
InputNumeric KeyAscii, Text1
End Sub
Top
4 楼ttksy(ziffish)回复于 2005-06-02 19:35:10 得分 0
谢谢,这样实现了不允许输入字符
有没有办法实现输入之后,单击按钮运算的时候再检查呢?Top
5 楼56625079(红狐狸)回复于 2005-06-02 19:48:43 得分 12
Private Sub Command1_Click()
If IsNumeric(Text1.Text) = True Then
MsgBox "数字"
Else
MsgBox "非数字"
End If
End Sub
Top
6 楼ttksy(ziffish)回复于 2005-06-02 20:10:24 得分 0
嗯,这个对了Top




