VB里面怎么设置一个过程现在只能输入数字。
答案:3 悬赏:0
解决时间 2021-02-04 17:59
- 提问者网友:失败的占卜者
- 2021-02-04 05:06
VB里面怎么设置一个过程现在只能输入数字。然后在多个文本框输入时调用这个验证,不对的等于没有输入。新手请把全部程序代码发上来谢谢了。
最佳答案
- 二级知识专家网友:青灯壁纸妹
- 2021-02-04 05:47
Sub KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyPress KeyAscii
End Sub
类会用吗?尝试一下下面的方法:
菜单:工程-添加类模块,类模块中代码如下:
Dim WithEvents Text1 As TextBox
Sub Attach(txt As TextBox)
Set Text1 = txt
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
Form1中代码如下:
Dim cls1 As New Class1
Private Sub Form_Load()
cls1.Attach Text1
End Sub
Form2中代码如下:
Dim cls1(1) As New Class1
Private Sub Form_Load()
cls1(0).Attach Text1
cls1(1).Attach Text2
End Sub
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyPress KeyAscii
End Sub
类会用吗?尝试一下下面的方法:
菜单:工程-添加类模块,类模块中代码如下:
Dim WithEvents Text1 As TextBox
Sub Attach(txt As TextBox)
Set Text1 = txt
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
Form1中代码如下:
Dim cls1 As New Class1
Private Sub Form_Load()
cls1.Attach Text1
End Sub
Form2中代码如下:
Dim cls1(1) As New Class1
Private Sub Form_Load()
cls1(0).Attach Text1
cls1(1).Attach Text2
End Sub
全部回答
- 1楼网友:短发女王川岛琦
- 2021-02-04 06:22
Sub CheckNum(Txt As TextBox)
Dim Temp As String
Static fTemp As String
Temp = Txt.Text
If IsNumeric(Temp) Then
Txt.Text = Temp
Else
Txt.Text = fTemp
End If
fTemp = Txt.Text
End Sub
Private Sub Text1_Change() '调用示例,检查text1的,text2以此类推
CheckNum Text1
Text1.SelStart = Len(Text1.Text)
End Sub
- 2楼网友:如果这是命
- 2021-02-04 06:10
private sub text1_keypress( keyascii as integer) '按键限制
select case keyascii
case 8 '退格键
'可以不输入处理代码
case 13 '回车键
'可以不输入处理代码
case 27 'esc键
'可以不输入处理代码
case 48 to 57
'输入的是数字
case else
keyascii=0 '不是以上的全部取消按键
end select
输入长度限制可以通过设置text控件的maxlength属性来限制
楼上的方法同样可行
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯