改变 VB TextBox 的风格,只能输入数字/大写/小写。

以下代码是运用API实现的一种方法,还有一种方法是利用TextBox本身的KeyPress过程来实现的。

Option Explicit
DefInt A-Z
'DefInt (字母范围) 定义整型 -> 所有以A-Z字母开头的都是整型变量

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( ByVal hWnd As Long , ByVal nIndex As Long , ByVal dwNewLong As Long ) As Long
Private Declare Function
GetWindowLong Lib "user32" Alias "GetWindowLongA" ( ByVal hWnd As Long , ByVal nIndex As Long ) As Long

Private Const
GWL_STYLE = - 16

Enum TxtBoxStyles
ES_UPPERCASE =
&H8 & '大写
ES_LOWERCASE = &H10 & '小写
ES_NUMBER = &H2000 & '数字
End Enum

Public Sub
SetTxtBoxStyle(hWnd As Long , InputStyle As TxtBoxStyles)
Dim lngStyle As Long
lngStyle = GetWindowLong(hWnd, GWL_STYLE)
lngStyle = lngStyle
Or InputStyle
SetWindowLong hWnd, GWL_STYLE, Style
End Sub


为了方便新手,我在这里将用KeyPress过程实现同样效果的方法简单说明一下。

Private Sub Text1_KeyPress(KeyAscii As Integer )
'显示出按下键的Ascii码
Debug.Print KeyAscii
'不给输入 "a"、"s"、"d" 这三个字母
If KeyAscii = 97 And KeyAscii = 115 And KeyAscii = 100 Then
'当按下asd这三个按键时屏蔽
KeyAscii = 0
End If
End Sub

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值