在Text控件的KeyPress事件中加入以下語句: Private Sub text1_KeyPress(KeyAscii As Integer) If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then If KeyAscii <> Asc(".") Then '可以輸入退位鍵“Backspace”“Ctrl+C”、“Ctrl+V”、“Shift+:”,"Ctrl+X" If KeyAscii <> 8 And KeyAscii <> 3 And KeyAscii <> 22 And KeyAscii <> 24 Then KeyAscii = 0 End If End If End If end sub
Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii > 57 Or KeyAscii < 48 Then KeyAscii = 0End Sub
Private Sub text1_KeyPress(KeyAscii As Integer) If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then KeyAscii = 0
End If End Sub
text的keypress事件 if keyascii=vbkeytab or key ascii=vbkeyback then exit sub dim a as string a=chr(keyasci) if isnumeric(a)=false then keyascii=0
1、用maskedit 2、在keydown里面检测处理 3、找专门的控件
Private Sub text1_KeyPress(KeyAscii As Integer) If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then KeyAscii = 0
End If End Sub
Private Sub text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case vbKeyBack, vbKeyTab, vbKeyReturn, Asc("0") To Asc("9") '允许BackSpase, Tab, Enter 和数字键 Case Else KeyAscii = 0 End Select End Sub
Public Function sffunLimitNumber(ByVal IntVal As Integer) As Integer '-------------------1------------------- '目 的:只允许在文本框内输入数字、退格、删除及回车键 '输 入:ByVal IntVal As Integer,任意的键值 '被传递值:无 '返 回 值:过滤后的键值 '输 出:无 '注 解: '用 法:在文本框的KeyPress事件中输入KeyAscii = sffunLimitNumber(KeyAscii)即可 '修 订 版: '-------------------1------------------- If (IntVal <> vbKeyDelete) _ And (IntVal <> vbKeyBack) _ And (IntVal <> 13) _ And (IntVal < 48 Or IntVal > 57) Then IntVal = 0 End If sffunLimitNumber = IntValEnd Function
Sub SetNumTextBox(ByVal Text As textbox) Dim lS As Long lS = GetWindowLong(Tex.hwndt, GWL_STYLE) lS = lS Or ES_NUMBER SetWindowLong Text.hwnd, GWL_STYLE, lS End Sub
Private Sub Text1_Change() If Not ValidateNumeric(Text1.Text) ThenText1.Text = ""End If End Sub Private Function ValidateNumeric(strText As String) As BooleanValidateNumeric = CBool(IsNumeric(strText))End Function
Private Sub text1_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
If KeyAscii <> Asc(".") Then
'可以輸入退位鍵“Backspace”“Ctrl+C”、“Ctrl+V”、“Shift+:”,"Ctrl+X"
If KeyAscii <> 8 And KeyAscii <> 3 And KeyAscii <> 22
And KeyAscii <> 24 Then
KeyAscii = 0
End If
End If
End If
end sub
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
End If
End Sub
if keyascii=vbkeytab or key ascii=vbkeyback then exit sub
dim a as string
a=chr(keyasci)
if isnumeric(a)=false then keyascii=0
2、在keydown里面检测处理
3、找专门的控件
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
End If
End Sub
Select Case KeyAscii
Case vbKeyBack, vbKeyTab, vbKeyReturn, Asc("0") To Asc("9")
'允许BackSpase, Tab, Enter 和数字键
Case Else
KeyAscii = 0
End Select
End Sub
'-------------------1-------------------
'目 的:只允许在文本框内输入数字、退格、删除及回车键
'输 入:ByVal IntVal As Integer,任意的键值
'被传递值:无
'返 回 值:过滤后的键值
'输 出:无
'注 解:
'用 法:在文本框的KeyPress事件中输入KeyAscii = sffunLimitNumber(KeyAscii)即可
'修 订 版:
'-------------------1-------------------
If (IntVal <> vbKeyDelete) _
And (IntVal <> vbKeyBack) _
And (IntVal <> 13) _
And (IntVal < 48 Or IntVal > 57) Then
IntVal = 0
End If
sffunLimitNumber = IntValEnd Function
Dim lS As Long
lS = GetWindowLong(Tex.hwndt, GWL_STYLE)
lS = lS Or ES_NUMBER
SetWindowLong Text.hwnd, GWL_STYLE, lS
End Sub
If Not ValidateNumeric(Text1.Text) ThenText1.Text = ""End If
End Sub
Private Function ValidateNumeric(strText As String) As BooleanValidateNumeric = CBool(IsNumeric(strText))End Function