Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 13 Then
Select Case KeyAscii
Case Asc("-") '只能在最左
If Text1.SelStart = 0 Then
If Left(Text1.Text, 1) = "-" Then
KeyAscii = 0
Beep
End If
Else
KeyAscii = 0
Beep
End If
Case Asc(vbBack)
'无变化,退格键不屏蔽
Case Asc(" ") '32
KeyAscii = 0
If Text1.SelLength > 0 Then
Text1.SelText = ""
End If
Case Asc(".") '46 Only One!!!!
If InStr(Text1.Text, ".") Then
KeyAscii = 0
End If
Case Is < Asc(0) '48
KeyAscii = 0
Case Is > Asc(9) '57
KeyAscii = 0
End Select
End Sub
If KeyAscii <> 13 Then
Select Case KeyAscii
Case Asc("-") '只能在最左
If Text1.SelStart = 0 Then
If Left(Text1.Text, 1) = "-" Then
KeyAscii = 0
Beep
End If
Else
KeyAscii = 0
Beep
End If
Case Asc(vbBack)
'无变化,退格键不屏蔽
Case Asc(" ") '32
KeyAscii = 0
If Text1.SelLength > 0 Then
Text1.SelText = ""
End If
Case Asc(".") '46 Only One!!!!
If InStr(Text1.Text, ".") Then
KeyAscii = 0
End If
Case Is < Asc(0) '48
KeyAscii = 0
Case Is > Asc(9) '57
KeyAscii = 0
End Select
End Sub
解决方案 »
- VB-PowerWrap4.1打包程序后运行缓慢 杀毒软件误报
- jmail 发送邮件,但是如果有一次没有发送出去,后续的就发送不了
- 如何使COM+应用程序作为服务运行
- 请问如何让text控件中的文字起始位置向右偏移几个像素?
- 怎样改变窗体标题栏和菜单的外观?
- vb下面有没有对二进制数据做位运算的符号或者函数
- 谁有VB用的条码生成的控件,给我一个?(急用)
- 如何使File控件显示出的文件不带后缀名?
- 怎样把TreeView 控件连接到服务器上的数据库
- 最后一个问题,小弟正在写一个服务程序,可是启动的时候报错。一定给分,急急急,我在线等回答{(1053 服务没有及时地响应启动或控制请求)
- SendMessage wm_copydata 是如何发送数据,以及如何取到发过来的数据??
- 如何用vb6.0编程使应用程序窗口在最大化时同步调整窗体大小?
方法一:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Key = Chr(KeyAscii)
If KeyAscii <> 8 And Key < "0" Or Key > "9" Then
MsgBox "请填入数字!"
End If
End Sub
这个方法仍然可以输入字母,只是一个提示作用。
方法二:
限制输入(只可输入数字)
KeyAscii = ValiText(KeyAscii, "0123456789", True)
在模块中
Public Function ValiText(KeyIn As Integer, ValidateString As String, Editable As Boolean) As Integer
Dim ValidateList As String
Dim KeyOut As Integer
If Editable = True Then
ValidateList = UCase(ValidateString) & Chr(8) & Chr(13)
Else
ValidateList = UCase(ValidateString)
End If
If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then
KeyOut = KeyIn
Else
KeyOut = 0
Beep
End If
ValiText = KeyOut
End Function