wpf文本框限制输入长度_access限制文本框输入的长度

在应用程序中,如姓名,号码等为了避免错误,我们有时会限制文本框录入的内容的长度。

在非绑定的文本框中,我们可以通过掩码来限制,但是这种方法很多弊端,这里先不作讨论

这里介绍一下用vba代码如何限制录入的长度

如图,在窗体中添加一个“text2”非绑定文本框

ffe49e647ecda897dd0a79f1cce217e7.png

将下面两个函数粘贴到模块中。

Sub LimitKeyPress(ctl As Control, iMaxLen As Integer, KeyAscii As Integer)On Error GoTo Err_LimitKeyPress ' Purpose: Limit the text in an unbound text box/combo. ' Usage: In the control's KeyPress event procedure: ' Call LimitKeyPress(Me.MyTextBox, 12, KeyAscii) ' Note: Requires LimitChange() in control's Change event also. If Len(ctl.Text) - ctl.SelLength >= iMaxLen Then If KeyAscii <> vbKeyBack Then KeyAscii = 0 Beep End If End IfExit_LimitKeyPress: Exit SubErr_LimitKeyPress: MsgBox "Error " & Err.Number & ": " & Err.Description Resume Exit_LimitKeyPressEnd SubSub LimitChange(ctl As Control, iMaxLen As Integer)On Error GoTo Err_LimitChange ' Purpose: Limit the text in an unbound text box/combo. ' Usage: In the control's Change event procedure: ' Call LimitChange(Me.MyTextBox, 12) ' Note: Requires LimitKeyPress() in control's KeyPress event also. If Len(ctl.Text) > iMaxLen Then MsgBox "不能超过" & iMaxLen & " 字符
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值