在线求助 100分相送

13520089720tianyu 2003-10-18 12:13:20
在text1.keypress中
1 只允许输入0——9的数值
2 并且只允许输入一个负号 及一个小数点
3 负号在第一位
...全文
22 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
onedream 2003-10-18
  • 打赏
  • 举报
回复
在KeyPress里用keycode判断,
如果不合适就让keycode=0

源码我就不写了啊
脆皮大雪糕 2003-10-18
  • 打赏
  • 举报
回复
Option Explicit
Private Sub Text1_Change()
If (Not IsNumeric(Text1.Text)) And (Text1.Text <> "-") Then
MsgBox "err"
End If
End Sub
阿建像熊猫 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
  • 打赏
  • 举报
回复
to chewinggum(口香糖·向星星前进) :
如果在文本框中输入类似下面的数值,你看你的程序会怎么样呢?:)
-1,22E-05
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

1,451

社区成员

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

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