是让textbox只允许输入数字吗?是的话,这样: 一个模块,一个窗体: 模块代码: Option Explicit Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Private Const WM_PASTE = &H302 Public Const GWL_STYLE = (-16) Public Const ES_NUMBER = &H2000 Private Const GWL_WNDPROC = (-4)Dim OldWndProc As LongPublic Sub StartSubclass(ByVal hwnd As Long) OldWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubClassProc) End Sub Public Sub EndSubclass(ByVal hwnd As Long) If OldWndProc <> 0 Then Call SetWindowLong(hwnd, GWL_WNDPROC, OldWndProc) OldWndProc = 0 End If End Sub Private Function SubClassProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Select Case wMsg Case WM_PASTE Debug.Print "吃掉了WM_PASTE消息" Case Else SubClassProc = CallWindowProc(OldWndProc, hwnd, wMsg, wParam, lParam) End Select End Function
窗体(有一个textbox)代码: Option Explicit Private Sub Form_Load() Dim style As Long style = GetWindowLong(Text1.hwnd, GWL_STYLE) style = style Or ES_NUMBER SetWindowLong Text1.hwnd, GWL_STYLE, style text1.text="" StartSubclass Text1.hwnd End Sub Private Sub Form_Unload(Cancel As Integer) EndSubclass Text1.hwnd End Sub
使用子类技术监控WM_PASTE消息,当有WM_PASTE发生时,再作控制
Option Explicit Dim t1 As StringPrivate Sub Form_Load() Text1 = "" End SubPrivate Sub Text1_Change() If Not IsNumeric(Text1) Then Text1 = t1 Else t1 = Text1 End If Text1.SelStart = Len(Text1) End SubPrivate Sub Text1_GotFocus() t1 = Text1 End Sub
Option Explicit Dim t1 As StringPrivate Sub Form_Load() Text1 = "" End SubPrivate Sub Text1_Change() If Not IsNumeric(Text1) Then Text1 = t1 Text1.SelStart = Len(Text1) Else t1 = Text1 End If End SubPrivate Sub Text1_GotFocus() t1 = Text1 End Sub
在textbox禁止输入文字和符号,但是用鼠标复制文字符号,然后粘贴上去,还是可以,怎么解决 -------------------------------- 在文本框的keypress事件了加如代码: Private Sub Text1_KeyPress(KeyAscii As Integer) keyascii=0 End Sub
SetWindowLong;GetWindowLong;CallWindowProc
一个模块,一个窗体:
模块代码:
Option Explicit
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_PASTE = &H302
Public Const GWL_STYLE = (-16)
Public Const ES_NUMBER = &H2000
Private Const GWL_WNDPROC = (-4)Dim OldWndProc As LongPublic Sub StartSubclass(ByVal hwnd As Long)
OldWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubClassProc)
End Sub
Public Sub EndSubclass(ByVal hwnd As Long)
If OldWndProc <> 0 Then
Call SetWindowLong(hwnd, GWL_WNDPROC, OldWndProc)
OldWndProc = 0
End If
End Sub
Private Function SubClassProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case wMsg
Case WM_PASTE
Debug.Print "吃掉了WM_PASTE消息"
Case Else
SubClassProc = CallWindowProc(OldWndProc, hwnd, wMsg, wParam, lParam)
End Select
End Function
窗体(有一个textbox)代码:
Option Explicit
Private Sub Form_Load()
Dim style As Long
style = GetWindowLong(Text1.hwnd, GWL_STYLE)
style = style Or ES_NUMBER
SetWindowLong Text1.hwnd, GWL_STYLE, style
text1.text=""
StartSubclass Text1.hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
EndSubclass Text1.hwnd
End Sub
Dim t1 As StringPrivate Sub Form_Load()
Text1 = ""
End SubPrivate Sub Text1_Change()
If Not IsNumeric(Text1) Then
Text1 = t1
Else
t1 = Text1
End If
Text1.SelStart = Len(Text1)
End SubPrivate Sub Text1_GotFocus()
t1 = Text1
End Sub
Dim t1 As StringPrivate Sub Form_Load()
Text1 = ""
End SubPrivate Sub Text1_Change()
If Not IsNumeric(Text1) Then
Text1 = t1
Text1.SelStart = Len(Text1)
Else
t1 = Text1
End If
End SubPrivate Sub Text1_GotFocus()
t1 = Text1
End Sub
--------------------------------
在文本框的keypress事件了加如代码:
Private Sub Text1_KeyPress(KeyAscii As Integer)
keyascii=0
End Sub
-----------------------------------------------------------
rainstormmaster(暴风雨 v2.0) 正解
rainstormmaster(暴风雨 v2.0)没错!!
1.拦截WM_PASTE消息,使控件接收到粘贴事件
2.将控件加上ES_NUMBER风格,使其只能接收数值
可是真正无Bug的、高效的字符串判断程序不是一次能写出来的最主要原因是VB没有封装“拦截消息”这个Windows编程中最基础的功能
所以得自己用SubClassing技术来拦截消息