急救 找大虾

13520089720tianyu 2003-10-18 12:15:12
在text1.keypress中
1 只允许输入0——9的数值
2 并且只允许输入一个负号 及一个小数点
3 负号在第一位
...全文
52 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiqunet 2004-02-29
  • 打赏
  • 举报
回复
较完美的,先贴出来,方便以后Copy来用,(下面代码依然未考虑科学计数法问题)
Select Case KeyAscii
Case 8
Case 13
Case 45
If Text0(Index).SelStart <> 0 Or (InStr(Text0(Index), "-") And Text0(Index).SelLength <> Len(Text0(Index))) > 0 Then KeyAscii = 0 '如果光标不在第一位,则取消负号的录入
Case 46
If InStr(Text0(Index), ".") > 0 Then
KeyAscii = 0 '只接收一个小数点“.”
ElseIf Text0(Index).SelStart = 0 Then
KeyAscii = 0
Text0(Index) = "0." & Text0(Index) '当文本框中没有任何字符或只有负号时输入小数点,则自动在小数点前加上“0”
Text0(Index).SelStart = Len(Text0(Index)) '将光标设在文本之后
ElseIf Left(Text0(Index), 1) = "-" And Text0(Index).SelStart = 1 Then
KeyAscii = 0
Text0(Index) = "-0." & Right(Text0(Index), Len(Text0(Index)) - 1)
Text0(Index).SelStart = Len(Text0(Index))
End If
Case Is < 48
KeyAscii = 0
Case 48
If Left(Text0(Index), 1) = "0" And Text0(Index).SelStart = 1 Then KeyAscii = 0
If Left(Text0(Index), 2) = "-0" And Text0(Index).SelStart = 2 Then KeyAscii = 0
Case Is > 57
KeyAscii = 0
End Select
qiqunet 2003-10-18
  • 打赏
  • 举报
回复
yoki(小马哥)

兄台的代码真好,简洁,呵呵

我在不断的调试中,呵呵,不过上面的是最终版本,呵呵 :D
qiqunet 2003-10-18
  • 打赏
  • 举报
回复
'这是带注释的,并且更改了负号的录入条件

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 45 And KeyAscii <> 46 Then '例外项(即允许退格键、回车键、负号键、小数点的输入)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 '取消非数字字符的输入
Else
If KeyAscii = 45 Then If Text1.SelStart <> 0 And InStr(Text1, "-") = 0 Then KeyAscii = 0 '如果光标不在第一位,则取消负号的录入
If KeyAscii = 46 Then
If InStr(Text1, ".") > 0 Then KeyAscii = 0 '只接收一个小数点“.”
If Text1 = "" Then '当文本框中没有任何字符或只有负号时输入小数点,则自动在小数点前加上“0”
Text1 = "0."
KeyAscii = 0
Text1.SelStart = Len(Text1) '将光标设在文本之后
End If
If Text1 = "-" Then
Text1 = "-0."
KeyAscii = 0
Text1.SelStart = Len(Text1)
End If

End If
End If
End Sub
yoki 2003-10-18
  • 打赏
  • 举报
回复
to: qiqunet(暗黑神话)
不好意思,看来你已经发现了
呵呵,新版本已经修正了我所说的那个bug了 :)
yoki 2003-10-18
  • 打赏
  • 举报
回复
我上面这个比较完善了

qiqunet(暗黑神话) 的如果先输123.34
然后再把光标移到首位输入"-"不能输入
qiqunet 2003-10-18
  • 打赏
  • 举报
回复
这是带注释的,并且更改了负号的录入条件

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 45 And KeyAscii <> 46 Then '例外项(即允许退格键、回车键、负号键、小数点的输入)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 '取消非数字字符的输入
Else
If KeyAscii = 45 Then If Text1.SelStart <> 0 Then KeyAscii = 0 '如果光标不在第一位,则取消负号的录入
If KeyAscii = 46 Then
If InStr(Text1, ".") > 0 Then KeyAscii = 0 '只接收一个小数点“.”
If Text1 = "" Then '当文本框中没有任何字符或只有负号时输入小数点,则自动在小数点前加上“0”
Text1 = "0."
KeyAscii = 0
Text1.SelStart = Len(Text1) '将光标设在文本之后
End If
If Text1 = "-" Then
Text1 = "-0."
KeyAscii = 0
Text1.SelStart = Len(Text1)
End If

End If
End If
End Sub
yoki 2003-10-18
  • 打赏
  • 举报
回复
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (Text1.SelStart <> 0 Or InStr(1, Text1.Text, "-") <> 0) And KeyAscii = 45 Then KeyAscii = 0
If InStr(1, Text1.Text, "-") <> 0 And Text1.SelStart = 0 Then KeyAscii = 0
If InStr(1, Text1.Text, ".") <> 0 And KeyAscii = 46 Then KeyAscii = 0
If (KeyAscii < 45 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
qiqunet 2003-10-18
  • 打赏
  • 举报
回复
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 45 And KeyAscii <> 46 Then
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
Else
If KeyAscii = 45 Then If Text1 <> "" Then KeyAscii = 0
If KeyAscii = 46 Then
If InStr(Text1, ".") > 0 Then KeyAscii = 0
If Text1 = "" Then
Text1 = "0."
KeyAscii = 0
Text1.SelStart = Len(Text1)
End If
If Text1 = "-" Then
Text1 = "-0."
KeyAscii = 0
Text1.SelStart = Len(Text1)
End If

End If
End If
End Sub
脆皮大雪糕 2003-10-18
  • 打赏
  • 举报
回复
IsNumeric 函数


返回 Boolean 值,指出表达式的运算结果是否为数。

语法

IsNumeric(expression)

必要的 expression 参数是一个 Variant,包含数值表达式或字符串表达式。

说明

如果整个 expression 的运算结果为数字,则 IsNumeric 返回 True;否则返回 False。

如果 expression 是日期表达式,则 IsNumeric 返回 False。
yoki 2003-10-18
  • 打赏
  • 举报
回复
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Len(Trim(Text1.Text)) > 0 And KeyAscii = 45 Then KeyAscii = 0
If InStr(1, Text1.Text, ".") <> 0 And KeyAscii = 46 Then KeyAscii = 0
If (KeyAscii < 45 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
脆皮大雪糕 2003-10-18
  • 打赏
  • 举报
回复
Option Explicit
Private Sub Text1_Change()
If (Not IsNumeric(Text1.Text)) And (Text1.Text <> "-") Then
MsgBox "err"
End If
End Sub
jackclh 2003-10-18
  • 打赏
  • 举报
回复
用ASC进行判断它的ASC值就行了,很简单的.
13520089720tianyu 2003-10-18
  • 打赏
  • 举报
回复
thanks yoki(小马哥)
13520089720tianyu 2003-10-18
  • 打赏
  • 举报
回复
thanks 回复人: rainstormmaster(rainstormmaster)
qiqunet(暗黑神话) ( ) 信誉:100
rainstormmaster(rainstormmaster) ( ) 信誉:125
rainstormmaster 2003-10-18
  • 打赏
  • 举报
回复
to chewinggum(口香糖·向星星前进) :
如果在文本框中输入类似下面的数值,你看你的程序会怎么样呢?:)
-1,22E-05
阿建像熊猫 2003-10-18
  • 打赏
  • 举报
回复
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (Text1.SelStart <> 0 Or InStr(1, Text1.Text, "-") <> 0) And KeyAscii = 45 Then KeyAscii = 0
If InStr(1, Text1.Text, "-") <> 0 And Text1.SelStart = 0 Then KeyAscii = 0
If InStr(1, Text1.Text, ".") <> 0 And KeyAscii = 46 Then KeyAscii = 0
If (KeyAscii < 45 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
KeyAscii < 45 Or KeyAscii > 57 这些是数字的ASCII。
KeyAscii = 8可以输入退格键。
rainstormmaster 2003-10-18
  • 打赏
  • 举报
回复
Dim haveD As Boolean


Private Sub Text1_KeyPress(KeyAscii As Integer)
If InStr(1, Text1.Text, ".") > 0 Then
haveD = True
Else
haveD = False
End If
If (haveD = True) And (KeyAscii = 46) Then
KeyAscii = 0
End If
If Text1.SelStart = 0 And InStr(1, Text1.Text, "-") > 0 Then
KeyAscii = 0
End If
If KeyAscii = 45 Then
If (InStr(1, Text1.Text, "-") = 0) And (Text1.SelStart = 0) Then
KeyAscii = 45
Else
KeyAscii = 0
End If
End If
If (KeyAscii > 57 Or KeyAscii < 48) And KeyAscii <> 46 And KeyAscii <> 45 And KeyAscii <> 13 And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
qiqunet 2003-10-18
  • 打赏
  • 举报
回复
上文还是错了,并且还是那个负号 :D
现作更改

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 45 And KeyAscii <> 46 Then '例外项(即允许退格键、回车键、负号键、小数点的输入)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 '取消非数字字符的输入
Else
If KeyAscii = 45 Then If Text1.SelStart <> 0 Or InStr(Text1, "-") > 0 Then KeyAscii = 0 '如果光标不在第一位,则取消负号的录入
If KeyAscii = 46 Then
If InStr(Text1, ".") > 0 Then KeyAscii = 0 '只接收一个小数点“.”
If Text1 = "" Then '当文本框中没有任何字符或只有负号时输入小数点,则自动在小数点前加上“0”
Text1 = "0."
KeyAscii = 0
Text1.SelStart = Len(Text1) '将光标设在文本之后
End If
If Text1 = "-" Then
Text1 = "-0."
KeyAscii = 0
Text1.SelStart = Len(Text1)
End If

End If
End If
End Sub

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧