出现错误后,程序还能回到误操作之前的状态
这段代码是控制TEXT1中只允许输入数字和小数点的:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If mf1.Col = 6 Or mf1.Col = 7 Or mf1.Col = 8 Then ‘mf1是MSFlexGrid
If (KeyAscii > 57 Or KeyAscii < 48) And KeyAscii <> 46 And KeyAscii <> 8 Then KeyAscii = 0
End If
End Sub
但有时误操作,输入两个小数点就不行了,怎样让其只能输入一个小数点,也像这样第二个小数点想字母一样根本就输不进去。
VB的程序一旦那里误操作出现错误就直接崩溃,只能将整个程序“结束”。能不能出现错误后,程序还能回到误操作之前的状态!
[解决办法]
http://blog.csdn.net/supermanking/article/details/2959044
http://blog.csdn.net/supermanking/article/details/2799053
[解决办法]
Private Sub Text1_KeyPress(KeyAscii As Integer) If mf1.Col = 6 Or mf1.Col = 7 Or mf1.Col = 8 Then 'mf1是MSFlexGrid ' If (KeyAscii > 57 Or KeyAscii < 48) And KeyAscii <> 46 And KeyAscii <> 8 Then KeyAscii = 0 Select Case KeyAscii Case 8 '跳过 值为8 Case 46 If InStr(Text1, ".") > 0 Then KeyAscii = 0 End If Case Is < 48 KeyAscii = 0 Case Is > 57 KeyAscii = 0 End Select End IfEnd Sub
[解决办法]
用 On error Resume Next 然后用 If not Err.Number then 判断是否发生错误,用 Err.Clear 清楚错误