读书人

改变 VB TextBox 的作派只能输入数字

发布时间: 2012-08-22 09:50:34 作者: rapoo

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

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

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过程实现同样效果的方法简单说明一下。

If KeyAscii = 97 And KeyAscii = 115 And KeyAscii = 100 Then
'当按下asd这三个按键时屏蔽
KeyAscii = 0
End If
End Sub

?

读书人网 >VB

热点推荐