●TextBox中如何限制只能输入金额●

chinabill 2004-03-22 10:28:33
功能:
1、TextBox 中只能输入0-9,回车键,删除键(←和Delete),小数点,Tab键。
2、此Textbox 主要用来输入代表金额的数字,同时能进行删除、回车等操作。
3、要求写成函数、过程形式,含注释。
4、以上三条全部满足者,给50-100分。
5、无聊者不要破坏贴子的完整。

本站地址:www.Emugua.net
...全文
120 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zss029 2004-03-23
  • 打赏
  • 举报
回复
晕呀
chinabill 2004-03-22
  • 打赏
  • 举报
回复

to _foo(void)
-----------------
请你自重!我是问过头了,只是我厌倦了某些人不停的UP。
现在你已经进圈套了,一切证明,你是无聊者!
上面带星的,虽然也是为了分的,但这是应该的,给分是一种鼓励!不然,你建议csdn经理撤消“给分”制度。
chinabill 2004-03-22
  • 打赏
  • 举报
回复
to _foo(void) //莫名函数:) AND hisofty(瘦马)
-----------------------------------------
二位语言不恰当!
你真的以为我不会结贴?
等着瞧吧。
  • 打赏
  • 举报
回复
这个更灵活些:
'***************************************************************************************
'* 函数名称:ValiText
'* 功能: 限制文本框的输入字符
'* 输入: keyin一般是KeyAscii,Validatestring能在文本框出现代字符,Editable决定[Backspace]键是否可用
'* 变量: Validatelist , Keyout
'* 返回:ValiText
'***************************************************************************************
'在 TEXT 的KeyPress事件调用,参数Validatestring即是TEXT中想要出现的字符
'参数Editable即是否使用[Backspace]键

Function ValiText(keyin As Integer, Validatestring As String, Editable As Boolean) As Integer
Dim Validatelist As String
Dim Keyout As Integer

If Editable = True Then
Validatelist = UCase(Validatestring) & Chr(8)
Else
Validatelist = UCase(Validatestring)
End If

If InStr(1, Validatelist, UCase(Chr(keyin)), 1) > 0 Then
Keyout = keyin
Else
Keyout = 0
End If
ValiText = Keyout
End Function

调用例子:

Private Sub txtMoney_KeyPress(KeyAscii As Integer)
KeyAscii = ValiText(KeyAscii, "0123456789.", True)
End Sub

Private Sub txtID_KeyPress(KeyAscii As Integer)
KeyAscii = ValiText(KeyAscii, "0123456789-abcdefghijklmnopqrstuvwxyz", True)
End Sub
rainstormmaster 2004-03-22
  • 打赏
  • 举报
回复
另外,你最好在子类中拦截一下wm_paste消息,不然通过复制粘贴,同样可以输入其他的字符
rainstormmaster 2004-03-22
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = mytest(KeyAscii)
End Sub
Private Function mytest(KeyAscii As Integer) As Integer
Dim s As String
s = "0123456789." + Chr(8) + Chr(13) '定义允许输入的字符
Dim inputs As String
inputs = Chr(KeyAscii) '当前输入的字符
If InStr(1, s, inputs) = 0 Then '如果当前输入的字符没有包含在字符串s中,则过滤输入
mytest = 0
Exit Function
End If
mytest = KeyAscii
End Function
hisofty 2004-03-22
  • 打赏
  • 举报
回复
就是!一天到晚拿分压人!谁稀罕!
_foo 2004-03-22
  • 打赏
  • 举报
回复
你以为你的50-100分是人民币啊?操.
无聊!
victorycyz 2004-03-22
  • 打赏
  • 举报
回复

private sub text1_keypress(keyascii as integer)

select case keyascii
case is<32
case 46
case 48 to 57
case else
keyascii=0
end select

end sub



另,问问题要客气一点。
strongfisher 2004-03-22
  • 打赏
  • 举报
回复

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 = IntVal

End Function

7,763

社区成员

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

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