Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0 End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii < vbkey0 Or KeyAscii > vbkey9) And KeyAscii >= 32 then keyascii=0 endif End Sub 不应该过滤掉小于32的键值 否则你就没法 Return 和 Tab 和作其他控制了
to bhhxd(天外来客) : 不过滤也不行,小于32的键值也有不是数字的吧,可以按住alt+数字来输入
这里有多种情况要求: 一、只允许录入整数: Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii < Asc(0) Or KeyAscii > Asc(9)) And KeyAscii <> 8 Then KeyAscii = 0 End If End Sub二、允许录入小数,且第一位不能为小数点Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii < Asc(0) Or KeyAscii > Asc(9)) And KeyAscii <> 8 And KeyAscii <> Asc(".") Then KeyAscii = 0 End If End SubPrivate Sub Text1_Change() If Not IsNumeric(Text1.Text) And Len(Text1.Text) > 0 Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) Text1.SelStart = Len(Text1.Text) End If End Sub三、允许录入小数,且第一位可以为小数点Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii < Asc(0) Or KeyAscii > Asc(9)) And KeyAscii <> 8 And KeyAscii <> Asc(".") Then KeyAscii = 0 End If End SubPrivate Sub Text1_Change() If Not IsNumeric(Text1.Text) And Len(Text1.Text) > 1 Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) Text1.SelStart = Len(Text1.Text) End If End Sub四、允许录入小数,且小数位数为n(n>0)位(此处允许第一位可以为小数点)Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii < Asc(0) Or KeyAscii > Asc(9)) And KeyAscii <> 8 And KeyAscii <> Asc(".") Then KeyAscii = 0 End If End Sub Private Sub Text1_Change() If Not IsNumeric(Text1.Text) And Len(Text1.Text) > 1 Then '此处允许第一位为小数点 Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) Text1.SelStart = Len(Text1.Text) End If If Len(Text1.Text) - InStr(1, Text1.Text, ".") > 4 Then '此处n=4 Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) Text1.SelStart = Len(Text1.Text) End If End Sub
试试这个 Private Sub Text1_Change() On Error Resume Next If Not IsNumeric(Text1.Text) Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) Text1.SelStart = Len(Text1.Text) End If End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii > Asc("9") Then KeyAscii = 0 End If End Sub 只要是数字都能输入,无论是小数,整数。
将kangshu的代码改在key_press中较好(如在text1_change()中加代码,任何时候打一小数点则通不过): Private Sub Text1_Keypress(KeyAscii As Integer) if keyascii =13 then On Error Resume Next
If Not IsNumeric(Text1.Text) Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) Text1.SelStart = Len(Text1.Text) End If end ifEnd Sub
对不起,上述代码还需修改: Private Sub Text1_Keypress(KeyAscii As Integer) On Error Resume Next
If Not IsNumeric(Text1.Text) Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) Text1.SelStart = Len(Text1.Text) End If End Sub
不会啊 在keypress中不好用 只能用在change()事件中 你试试看
TO qingming81(晴明): 我试过了可以输入一个小数点,只能输一个
这个问题昨晚我调试了代码,应该如下: Option Explicit Dim decimaltag As BooleanPrivate Sub Form_Load() decimaltag = False End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 46 And decimaltag = False Then decimaltag = True '控制小数点只能输入一次。 ElseIf KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Then Exit Sub Else KeyAscii = 0 End If End Sub顺便说一句,上述代码经过修改,可以限定输入大写字母、小写字母,或仅输入如A、B、C三个字母或你指定的任何符号,只需将相关字符的代码加入if或Elseif条件中。另外,书上的作法是(较为烦琐):将Form1的Kewpreview属性设置为True(以此拦截屏幕输入) 在text1_keypress中加下述代码:keyascii =0 在Form1_Keypress加下述代码: If KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Then text1.text=text1.text & chr$(keyascii) text1.selstart=len(text1.text) end if另致Kangsu:如果一开始就打小数点,就会出现问题。
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
If (KeyAscii < vbkey0 Or KeyAscii > vbkey9) And KeyAscii >= 32 then
keyascii=0
endif
End Sub
不应该过滤掉小于32的键值
否则你就没法 Return 和 Tab 和作其他控制了
不过滤也不行,小于32的键值也有不是数字的吧,可以按住alt+数字来输入
一、只允许录入整数:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < Asc(0) Or KeyAscii > Asc(9)) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub二、允许录入小数,且第一位不能为小数点Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < Asc(0) Or KeyAscii > Asc(9)) And KeyAscii <> 8 And KeyAscii <> Asc(".") Then
KeyAscii = 0
End If
End SubPrivate Sub Text1_Change()
If Not IsNumeric(Text1.Text) And Len(Text1.Text) > 0 Then
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End Sub三、允许录入小数,且第一位可以为小数点Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < Asc(0) Or KeyAscii > Asc(9)) And KeyAscii <> 8 And KeyAscii <> Asc(".") Then
KeyAscii = 0
End If
End SubPrivate Sub Text1_Change()
If Not IsNumeric(Text1.Text) And Len(Text1.Text) > 1 Then
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End Sub四、允许录入小数,且小数位数为n(n>0)位(此处允许第一位可以为小数点)Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < Asc(0) Or KeyAscii > Asc(9)) And KeyAscii <> 8 And KeyAscii <> Asc(".") Then
KeyAscii = 0
End If
End Sub
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) And Len(Text1.Text) > 1 Then '此处允许第一位为小数点
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
If Len(Text1.Text) - InStr(1, Text1.Text, ".") > 4 Then '此处n=4
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End Sub
Private Sub Text1_Change()
On Error Resume Next
If Not IsNumeric(Text1.Text) Then
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End Sub
If KeyAscii > Asc("9") Then
KeyAscii = 0
End If
End Sub
只要是数字都能输入,无论是小数,整数。
Private Sub Text1_Keypress(KeyAscii As Integer)
if keyascii =13 then
On Error Resume Next
If Not IsNumeric(Text1.Text) Then
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If end ifEnd Sub
Private Sub Text1_Keypress(KeyAscii As Integer)
On Error Resume Next
If Not IsNumeric(Text1.Text) Then
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End Sub
我试过了可以输入一个小数点,只能输一个
Option Explicit
Dim decimaltag As BooleanPrivate Sub Form_Load()
decimaltag = False
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 46 And decimaltag = False Then
decimaltag = True '控制小数点只能输入一次。
ElseIf KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Then
Exit Sub
Else
KeyAscii = 0
End If
End Sub顺便说一句,上述代码经过修改,可以限定输入大写字母、小写字母,或仅输入如A、B、C三个字母或你指定的任何符号,只需将相关字符的代码加入if或Elseif条件中。另外,书上的作法是(较为烦琐):将Form1的Kewpreview属性设置为True(以此拦截屏幕输入)
在text1_keypress中加下述代码:keyascii =0
在Form1_Keypress加下述代码:
If KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Then
text1.text=text1.text & chr$(keyascii)
text1.selstart=len(text1.text)
end if另致Kangsu:如果一开始就打小数点,就会出现问题。