有个文本框职能限定输入正整数文本框初始值为0
我在LostFocus事件里写的判断语句:
Private Sub Text1_LostFocus(Index As Integer) If VarType(Text1(Index).Text) <> 2 Then ' 判断是否是整数
MsgBox ("请输入整数!")
Text1(Index).SetFocus
Exit Sub
End IfEnd Sub结果问题就是:不改该文本框的内容(默认值为0),不报错
改了文本框里的数值,不管是正整数、还是其他的字符,失去焦点时都报MsgBox ("请输入整数!")不知道怎么回事?请帮帮我,我没学过VB,这都是我自己摸索的,谢谢各位!
我在LostFocus事件里写的判断语句:
Private Sub Text1_LostFocus(Index As Integer) If VarType(Text1(Index).Text) <> 2 Then ' 判断是否是整数
MsgBox ("请输入整数!")
Text1(Index).SetFocus
Exit Sub
End IfEnd Sub结果问题就是:不改该文本框的内容(默认值为0),不报错
改了文本框里的数值,不管是正整数、还是其他的字符,失去焦点时都报MsgBox ("请输入整数!")不知道怎么回事?请帮帮我,我没学过VB,这都是我自己摸索的,谢谢各位!
If keyasii < 48 Or KeyAscii > 57 Then
MsgBox ("请输入整数!")
Text1(Index).SetFocus End Sub
直接控制只能输入数字,而且不能以0开头就可以了
就text1(4)是要求输入正整数的
if index=4 then
If keyasii < 48 Or KeyAscii > 57 Then
MsgBox ("请输入整数!")
Text1(Index).SetFocus
end if
end if
End Sub
Option ExplicitPrivate Sub Form_Load()
Text1.Text = vbNullChar
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 Then
Dim re
Set re = CreateObject("VBScript.RegExp")
re.IgnoreCase = True
re.Global = True
re.Pattern = "^([1-9](\d+)?)?$"
If Not re.test(Text1.Text & Chr(KeyAscii)) Then
KeyAscii = 0
End If
End If
End Sub
If Index = 4 Then
If KeyAscii < 48 Or KeyAscii > 57 Then
MsgBox ("请输入整数!")
KeyAscii = 0
End If
End IfEnd Sub
KeyAscii 该如何限制?
If Index = 4 Then
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 Then
MsgBox ("请输入整数!")
KeyAscii = 0
End If
End IfEnd Sub