为什么这个vb的text只能输入两位数字!

来源:百度知道 编辑:UC知道 时间:2024/07/04 02:43:37
Dim cleardisplay As Boolean

Private Sub Command1_Click(Index As Integer)
If cleardisplay Then
Text1.Text = ""
clesrdisplay = False
End If
If Val(Text1.Text) < 10 Then
Text1.Text = Text1.Text + Command1(Index).Caption

End If
End Sub

这是一个简单计算器里面的代码,为什么只能输入两位数字?

很简单,因为你弄错了一个函数,你用val(text1.text)<10的本意可能是想检测小于10位的录入数字吧,但是用错了,因为你随便录入到第二个数字时就可能已经>10了,所以下面的语句不会生效了,TEXT1里也就没有数字显示,你要改用LEN来检测长度就行。正确的语句是:
Dim cleardisplay As Boolean

Private Sub Command1_Click(Index As Integer)
If cleardisplay Then
Text1.Text = ""
clesrdisplay = False
End If

If Len(Text1.Text) < 10 Then
Text1.Text = Text1.Text + Command1(Index).Caption
End If
End Sub

不懂你的意思

TEXT的属性设置中有个 Maxlength 设为2了,