Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Const GWL_STYLE = (-16) Const ES_NUMBER = &H2000&Public Sub SetNumber(NumberText As TextBox, Optional Flag As Boolean = True) Dim curstyle As Long, newstyle As Long 'retrieve the window style curstyle = GetWindowLong(NumberText.hwnd, GWL_STYLE) If Flag Then curstyle = curstyle Or ES_NUMBER Else curstyle = curstyle And (Not ES_NUMBER) End If 'Set the new style newstyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, curstyle) 'refresh NumberText.Refresh End Sub 把上面的写入一个模块中,在程序中使用: SetNumber text1 这样就可以达到你要的拉,使用 SetNumber text1 FASLE 就可以解除拉。
Private Sub txtFloor_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case Asc("-") '允许负数 If txtFloor.SelStart = 0 Then If Left(txtFloor.Text, 1) = "-" Then KeyAscii = 0 End If Else KeyAscii = 0 End If Case 8 '无变化,退格键不屏蔽 Case Asc(" ") '32 If txtFloor.SelLength = 0 Then KeyAscii = 0 End If Case Asc(".") '46 '允许小数点 If InStr(txtFloor.Text, ".") Then KeyAscii = 0 End If Case Is < Asc(0) '48 KeyAscii = 0 Case Is > Asc(9) '57 KeyAscii = 0 End Select End Sub
Public Sub CheckNum(KeyAscii As Integer) As Boolean '---------控制数字字符输入-------------- If (KeyAscii < 48 Or KeyAscii > 57) And _ KeyAscii <> 46 And KeyAscii <> 8 Then KeyAscii = 0 End Sub在需要的地方(KeyPress事件)调用即可。 若还需控制输入"-",加入"And KeyAscii<>Asc("-")"即可。
Sorry,原来是函数,现改成过程没删完。再来一次:Public Sub CheckNum(KeyAscii As Integer) '---------控制数字字符输入-------------- If (KeyAscii < 48 Or KeyAscii > 57) And _ KeyAscii <> 46 And KeyAscii <> 8 And KeyAscii<>Asc("-") Then KeyAscii = 0 End Sub
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const GWL_STYLE = (-16)
Const ES_NUMBER = &H2000&Public Sub SetNumber(NumberText As TextBox, Optional Flag As Boolean = True)
Dim curstyle As Long, newstyle As Long 'retrieve the window style
curstyle = GetWindowLong(NumberText.hwnd, GWL_STYLE) If Flag Then
curstyle = curstyle Or ES_NUMBER
Else
curstyle = curstyle And (Not ES_NUMBER)
End If 'Set the new style
newstyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, curstyle)
'refresh
NumberText.Refresh
End Sub
把上面的写入一个模块中,在程序中使用: SetNumber text1 这样就可以达到你要的拉,使用 SetNumber text1 FASLE 就可以解除拉。
Select Case KeyAscii
Case Asc("-") '允许负数
If txtFloor.SelStart = 0 Then
If Left(txtFloor.Text, 1) = "-" Then
KeyAscii = 0
End If
Else
KeyAscii = 0
End If
Case 8
'无变化,退格键不屏蔽
Case Asc(" ") '32
If txtFloor.SelLength = 0 Then
KeyAscii = 0
End If
Case Asc(".") '46 '允许小数点
If InStr(txtFloor.Text, ".") Then
KeyAscii = 0
End If
Case Is < Asc(0) '48
KeyAscii = 0
Case Is > Asc(9) '57
KeyAscii = 0
End Select
End Sub
'---------控制数字字符输入--------------
If (KeyAscii < 48 Or KeyAscii > 57) And _
KeyAscii <> 46 And KeyAscii <> 8 Then KeyAscii = 0
End Sub在需要的地方(KeyPress事件)调用即可。
若还需控制输入"-",加入"And KeyAscii<>Asc("-")"即可。
'---------控制数字字符输入--------------
If (KeyAscii < 48 Or KeyAscii > 57) And _
KeyAscii <> 46 And KeyAscii <> 8 And KeyAscii<>Asc("-") Then KeyAscii = 0
End Sub
呵呵,我的那个吗...不是流行叫“专业”吗?虽然繁点,但如果界面上有不止一个这样的TEXTBOX时用起来就比较简单了;要是在特定时候这些TEXTBOX又允许什么都可以输入拉,那我这段代码简直就是绝选拉!!呵呵,自己给自己打广告。
If KeyCode <> 你所想要的字符ASCII码 Then KeyCode = 0
End Sub
用ASCII码的朋友,你们有一点一定没试过。
你们的办法,用小键盘是没法输入的。
你好好试试,小键盘的9和大键盘上的i ASCII码是一样的。
相对应的。小键盘的0-9,应该都和大键盘上的一个字母相同。
Hothehe:老兄,你可有好几个帖子都不给分啦,算啦,以后我也不会再帮你啦!