怎样为VB窗体中的所有文本框限制只输入数字?
我知道单一个文本框是这样写PrivateSubtext1_KeyPress(KeyAsciiAsInteger)IfKeyAscii=46AndNotCBool(InSt...
我知道单一个文本框是这样写
Private Sub text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 46 And Not CBool(InStr(text1, ".")) Then Exit Sub
If KeyAscii = 8 Then Exit Sub
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub 展开
Private Sub text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 46 And Not CBool(InStr(text1, ".")) Then Exit Sub
If KeyAscii = 8 Then Exit Sub
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub 展开
3个回答
展开全部
除非你的所有文本框属于同一个控件数组,否则的话就要给每个文本框都写个同样的事件代码了(当然也可以做个自定义过程稍微简化一下)
如果是控件数组,那么只需一个事件过程即可:
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
'...依此类推
展开全部
Private Sub Text1_LostFocus() ‘鼠标移开文本框1
If Not IsNumeric(Text1) Then ’如果文本框1里的内容非数字
Text1.Text = "" ‘文本框1清空
End If
End Sub
依次类推
If Not IsNumeric(Text1) Then ’如果文本框1里的内容非数字
Text1.Text = "" ‘文本框1清空
End If
End Sub
依次类推
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把中间的代码整合成一个过程,然后逐个引用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询