VB中的text问题

来源:百度知道 编辑:UC知道 时间:2024/06/02 01:48:57
我想要限制text中输入的内容只能为数字\Integer,并且只能为0-255之间的数字(包括0和255),如果输入的数字大于255,但是text显示仍为255,其它的程序仍运行,小于0也一样
该如何写,麻烦注释一下,我才学一个星期

'新建一个text控件

Private Sub Form_Load()
Text1.MaxLength = 3
Text1.Text = ""
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = IIf(KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Or KeyAscii = 8, KeyAscii, 0)
End Sub

Private Sub Text1_Change()
If Text1.Text = "" Then Text1.Text = 0
If Text1.Text > 255 Then
Text1.Text = 255
Text1.SetFocus
End If
End Sub

if isnumeric(text1) and 0<=val(text1.text)<=255 then
....
else
msgbox("请输入0-255的数字")
end if

Private Sub Text1_Change()
If CInt(Text1.Text) > 255 Then Text1.Text = 255
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 32 Then Exit Sub
If KeyAscii > 57 Or KeyAscii < 48 Then KeyAscii = 0
End Sub

'没让输-或者.,所以不会出小数和负数<