2024-06-03 05:05:35
除非你的所有文本框属于同一个控件数组,否则的话就要给每个文本框都写个同样的事件代码了(当然也可以做个自定义过程稍微简化一下)
如果是控件数组,那么只需一个事件过程即可:
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii = 46 And InStr(Text1(Index), ".")) = 0 Then Exit Sub
If KeyAscii = 8 Then Exit Sub
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
如果不是控件数组,那么就只能这样:
Function chkkey(t As String, k As Integer) As Integer
chkkey = k
If k = 46 And InStr(t, ".")) = 0 Then Exit Function
If k = 8 Then Exit Function
If k < 48 Or k > 57 Then chkkey = 0
End Function
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = chkkey(Text1.Text, KeyAscii)
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
KeyAscii = chkkey(Text2.Text, KeyAscii)
End Sub
Private Sub Text3_KeyPress(KeyAscii As Integer)
KeyAscii = chkkey(Text3.Text, KeyAscii)
End Sub
'...依此类推
2024-06-03 20:58:48
2024-06-03 17:41:20