自动辨别约束

jumay 2004-07-14 12:40:00
有一个文本框txt1.text,它只能接受0--9的数字和小数点,其它的字符都不接受.请问代码要怎样写.在线等待.
...全文
144 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
BlueBeer 2004-07-14
  • 打赏
  • 举报
回复
Private Sub Text1_KeyPress(KeyAscii As Integer)
Const xStr As String = "0123456789"
KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)), KeyAscii, 0)
End Sub
Andy__Huang 2004-07-14
  • 打赏
  • 举报
回复
Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If

End Sub
TechnoFantasy 2004-07-14
  • 打赏
  • 举报
回复
在change事件中用IsNumeric判断输入的是不是数字。
Andy__Huang 2004-07-14
  • 打赏
  • 举报
回复
再加一句:
If KeyAscii = 46 And InStr(Text1.Text, ".") > 0 Then
KeyAscii = 0
End If

因為只能接受小數點一次。
BlueBeer 2004-07-14
  • 打赏
  • 举报
回复
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim xStr As String
xStr = "0123456789." & Chr(8) & Chr(13)
KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)), KeyAscii, 0)
End Sub
Andy__Huang 2004-07-14
  • 打赏
  • 举报
回复
再次修改:

If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then
KeyAscii = 0
End If

呵呵,這樣沒問題了吧?
liuxiaowang 2004-07-14
  • 打赏
  • 举报
回复
private sub text1_change()
dim LastChar as string
LastChar=right(text1.text,1)
if isnumeric(LastChar)=false then
Text1.text=left(text1.text,len(text1.text)-1)
end if
end sub
yanli 2004-07-14
  • 打赏
  • 举报
回复
Public Function In_single(KeyAscii As Integer) As Boolean
Dim ch_accept_single(20) As String
ch_accept_single(0) = "0"
ch_accept_single(1) = "1"
ch_accept_single(2) = "2"
ch_accept_single(3) = "3"
ch_accept_single(4) = "4"
ch_accept_single(5) = "5"
ch_accept_single(6) = "6"
ch_accept_single(7) = "7"
ch_accept_single(8) = "8"
ch_accept_single(9) = "9"
ch_accept_single(10) = "."
ch_accept_single(11) = "-"
ch_accept_single(12) = Chr(8)
In_single = False
For i = 0 To 12
If Chr(KeyAscii) = ch_accept_single(i) Then
In_single = True
End If
Next
End Function

Private Sub Text1_KeyPress(KeyAscii As Integer)
If In_single(KeyAscii) = False Then
KeyAscii = 0
End If
End Sub
jumay 2004-07-14
  • 打赏
  • 举报
回复
删除键和Del键还是不能用,请你再写一点好吗?
Andy__Huang 2004-07-14
  • 打赏
  • 举报
回复
補充:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> 46 Then
KeyAscii = 0
End If
End If
End Sub
這樣不是OK了麼?





jumay 2004-07-14
  • 打赏
  • 举报
回复
大家都忽略一个问题:小数点,Enter键,删除键,Tab键都不能使用,请大家继续完成我的问题

7,763

社区成员

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

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