本人发现的另类方法,谨供参考。Private Sub Text1_LostFocus() If LTrim(Str(Val(Text1))) <> Text1 Then MsgBox "请输入正确的数值。" Text1.SetFocus End If End Sub
to :lnhsgj(黑鹰) text1.text=12.7890 会怎样?
如此简单的问题???!!!!!! Private Sub Text1_KeyPress(KeyAscii As Integer) Mytext = "-0123456789." KeyAscii = TestText(KeyAscii, (Mytext), True) 'Text1只接受mytext规定的字符。 If KeyAscii > 90 Then KeyAscii = KeyAscii - 32 End SubFunction TestText(KeyIn As Integer, ListString As String, EditBasp As Boolean) As Integer Dim TestDATList As String '定义限制字符表变量 Dim KeyOut As Integer '返回值变量 If EditBasp = True Then '测试BACKSPACE键是否有效 TestDATList = UCase(ListString) & Chr(8) '得到含BACKSPACE键字符的大写表 Else TestDATList = UCase(ListString) '得到无BACKSPACE键字符的大写表 End If If InStr(1, TestDATList, UCase(Chr(KeyIn)), 1) > 0 Then '键值是否在表中 KeyOut = KeyIn '是则附键值 Else KeyOut = 0 '否则键值无效 End If TestText = KeyOut '返回结果 KeyOut = UCase(KeyOut) End Function 天才的我哈哈!!
通用检测模块'***************************************************************** '文本框只能输入数字Public Sub InputNumeric(KeyAscii As Integer, txtItem As TextBox) Select Case KeyAscii Case Asc("-") '允许负数 If txtItem.SelStart = 0 Then If Left(txtItem.Text, 1) = "-" Then KeyAscii = 0 Beep End If Else KeyAscii = 0 Beep End If Case 8 '无变化,退格键不屏蔽 Case Asc(" ") '32 If txtItem.SelLength = 0 Then KeyAscii = 0 End If Case Asc(".") '46 '允许小数点 If InStr(txtItem.Text, ".") Then KeyAscii = 0 End If Case Is < Asc(0) '48 KeyAscii = 0 Case Is > Asc(9) '57 KeyAscii = 0 End Select End Sub '*****************************************************************调用方式 在text的keypress事件中添加InputNumeric keyascii,textname即可
to lxcc(虫子) 你的方法虽然不错,但是我还是觉的我的好一些,只要把允许的字符放在Mytext里就行了,嘿嘿~~
lxcc(虫子) 的方法不错哟,我一直用这个。
稍微改进了一点Option Explicit'***************************************************************** '文本框只能输入数字Public Sub CheckInputNumericFormat(KeyAscii As Integer, txtItem As TextBox, FlagType As String, UnSigned As Boolean) Select Case KeyAscii Case Asc("-") '允许负数 If UnSigned = True And txtItem.SelStart = 0 Then If Left(txtItem.Text, 1) = "-" Then KeyAscii = 0 Beep End If Else KeyAscii = 0 Beep End If Case 8 '无变化,退格键不屏蔽 ' Case Asc(" ") '32 ' If txtItem.SelLength = 0 Then ' KeyAscii = 0 ' End If Case Asc(".") '46 '允许小数点 If FlagType = "Single" Then If InStr(txtItem.Text, ".") Then KeyAscii = 0 End If Else KeyAscii = 0 End If Case Is < Asc(0) '48 KeyAscii = 0 Case Is > Asc(9) '57 KeyAscii = 0 End Select End Sub '*****************************************************************Private Sub Text1_KeyPress(KeyAscii As Integer) Call CheckInputNumericFormat(KeyAscii, Me.ActiveControl, "Single", True) End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) '.......... If KeyAscii = Asc("-") Then Text1 = 0 - Val(Text1) KeyAscii = 0 End If end sub另类,又是另类,呵呵。
判断第低一个字符输入的第一个是否为‘-’就行了。用 left(XXX.text,1)取出左边第一个字符
keyPress事件中判断是否有"-",
如果有而且len(text1.text)>0,则屏蔽'-'键;
如果没有但是len(text1.text)>0,也屏蔽'-'键;
如果len(text1.text)=0,取消屏蔽'-'键
判断当前光标位置是否第一位
如果是判断text1.text中是否已经有"-"已经有屏蔽'-'键
如果不是第一位就屏蔽'-'键
小数点判断也是一样
If LTrim(Str(Val(Text1))) <> Text1 Then
MsgBox "请输入正确的数值。"
Text1.SetFocus
End If
End Sub
text1.text=12.7890
会怎样?
Private Sub Text1_KeyPress(KeyAscii As Integer)
Mytext = "-0123456789."
KeyAscii = TestText(KeyAscii, (Mytext), True) 'Text1只接受mytext规定的字符。
If KeyAscii > 90 Then KeyAscii = KeyAscii - 32
End SubFunction TestText(KeyIn As Integer, ListString As String, EditBasp As Boolean) As Integer
Dim TestDATList As String '定义限制字符表变量
Dim KeyOut As Integer '返回值变量
If EditBasp = True Then '测试BACKSPACE键是否有效
TestDATList = UCase(ListString) & Chr(8) '得到含BACKSPACE键字符的大写表
Else
TestDATList = UCase(ListString) '得到无BACKSPACE键字符的大写表
End If
If InStr(1, TestDATList, UCase(Chr(KeyIn)), 1) > 0 Then '键值是否在表中
KeyOut = KeyIn '是则附键值
Else
KeyOut = 0 '否则键值无效
End If
TestText = KeyOut '返回结果
KeyOut = UCase(KeyOut)
End Function
天才的我哈哈!!
'文本框只能输入数字Public Sub InputNumeric(KeyAscii As Integer, txtItem As TextBox)
Select Case KeyAscii
Case Asc("-") '允许负数
If txtItem.SelStart = 0 Then
If Left(txtItem.Text, 1) = "-" Then
KeyAscii = 0
Beep
End If
Else
KeyAscii = 0
Beep
End If
Case 8
'无变化,退格键不屏蔽
Case Asc(" ") '32
If txtItem.SelLength = 0 Then
KeyAscii = 0
End If
Case Asc(".") '46 '允许小数点
If InStr(txtItem.Text, ".") Then
KeyAscii = 0
End If
Case Is < Asc(0) '48
KeyAscii = 0
Case Is > Asc(9) '57
KeyAscii = 0
End Select
End Sub
'*****************************************************************调用方式
在text的keypress事件中添加InputNumeric keyascii,textname即可
'文本框只能输入数字Public Sub CheckInputNumericFormat(KeyAscii As Integer, txtItem As TextBox, FlagType As String, UnSigned As Boolean)
Select Case KeyAscii
Case Asc("-") '允许负数
If UnSigned = True And txtItem.SelStart = 0 Then
If Left(txtItem.Text, 1) = "-" Then
KeyAscii = 0
Beep
End If
Else
KeyAscii = 0
Beep
End If
Case 8
'无变化,退格键不屏蔽
' Case Asc(" ") '32
' If txtItem.SelLength = 0 Then
' KeyAscii = 0
' End If
Case Asc(".") '46 '允许小数点
If FlagType = "Single" Then
If InStr(txtItem.Text, ".") Then
KeyAscii = 0
End If
Else
KeyAscii = 0
End If
Case Is < Asc(0) '48
KeyAscii = 0
Case Is > Asc(9) '57
KeyAscii = 0
End Select
End Sub
'*****************************************************************Private Sub Text1_KeyPress(KeyAscii As Integer)
Call CheckInputNumericFormat(KeyAscii, Me.ActiveControl, "Single", True)
End Sub
'..........
If KeyAscii = Asc("-") Then
Text1 = 0 - Val(Text1)
KeyAscii = 0
End If
end sub另类,又是另类,呵呵。