ql-dz.com 发表于 2009-5-4 19:41:15

如何控制VB6.0的文本框中只能输入某一范围内的数字?

如何控制VB6.0的文本框中只能输入数字?
1.只能输入数字,可以由下面的代码实现..
Private Sub Text1_KeyPress(KeyAscii As Integer)   
Dim Numbers As String
Numbers = "1234567890" + Chr(8) + Chr(46)   
If InStr(Numbers, Chr(KeyAscii)) = 0 Then
KeyAscii = 0   
End If
End Sub

现需要解决下列问题:
2.只能输入一定范围的数字..比如限定只能输入0-65535之间的数字.
3.只能输入特定倍数的数字...比如手机号码,为11位...

应该怎么实现呢??初学,请指点下啊..希望给点具体的代码.

zxq6 发表于 2009-5-4 19:51:06

可以在onchange事件中来判断增加的那个字符的ascii码。

xiao_dian_dian 发表于 2009-5-6 00:41:52

Private Sub Text1_KeyPress(KeyAscii As Integer)



If KeyAscii > Asc("0") And KeyAscii < Asc("9") Then

      If Val(Text1 & Chr(KeyAscii)) > 65535 Then
               KeyAscii = 0
      End If


Else

'   If KeyAscii <> vbKeyBack Then
'
'         KeyAscii = 0
'   End If
   
   
   If KeyAscii <> vbKeyBack And KeyAscii <> 13 Then
   
         KeyAscii = 0
   End If
   
End If




End Sub



End Sub

aaron96031 发表于 2009-5-6 10:03:42

同意楼上的

ql-dz.com 发表于 2009-5-8 10:48:12

OK..学习啦.
页: [1]
查看完整版本: 如何控制VB6.0的文本框中只能输入某一范围内的数字?