关于VB问题?(高人请)急!急

来源:百度知道 编辑:UC知道 时间:2024/05/28 05:09:05
在Text1中输入大于10的数,就显示出123,小于就显示987。代码如下:
Dim a As Single

a = Val(Text1.Text)
If Text1.Text = "" Then
MsgBox "²不能为空", "48", “注意"
Text1.SetFocus
Exit Sub
End If
If Not IsNumeric(Text1.Text) Then
MsgBox 必为数字", "48", "注意"
Text1.Text = ""
Text1.SetFocus
Exit Sub
End If
If a >= 10 Then

Text2.Text = "123"
End If
If a < 10 Then
Text2.Text = "987"
End If

问题,当输入:“1+”,“1-”“9+”等,时它却仍然显示“987”为什么?可以改吗?
详细点,只要能改我还可以加分!
朋友回答的可以,但例如“1.2”,“1.5"等,小数的点的却没办法了,

在text1.text keypress 中输入以下代码,意思为除了0 - 9 , backspace和"."
有效外,其他键盘按钮无效。(让用户不会输入错误!)

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Then Exit Sub
If KeyAscii = Asc(".") Then Exit Sub
If KeyAscii = 8 Then Exit Sub
Beep
KeyAscii = 0
End Sub

之后在command click事件加上以下代码

If Text1.Text = "" Then
MsgBox "不能为空", "48", “注意"
Text1.SetFocus
Exit Sub
End If

Private Sub Command1_Click()
a = Text1.Text
If a >= 10 Then
Text2.Text = "123"
ElseIf a < 10 Then
Text2.Text = "987"
End If

你要先对text1.text的值进行判断,要先判断它是不是数值,不是则返回错误!你要是输入9+,通过VAL就强制弄成数值了!

试试这个,稍修改了一些小数点错误,不保证不存在错误

Private Sub Command1_Click()
Dim a As Single, j As Integer

a = Val(Text1.Text)
If Text1.Text = &q