改变 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

猜你喜欢

转载自yeuego.iteye.com/blog/947502
今日推荐