try::Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 93 Then
With Text1
.Enabled = False
.Enabled = True
.SetFocus
SendKeys "%"
End With
End If
End Sub
If KeyCode = 93 Then
With Text1
.Enabled = False
.Enabled = True
.SetFocus
SendKeys "%"
End With
End If
End Sub
Private txtBoxSelStart As Integer
Private txtBoxText As String
Private Sub Text1_Change()
If Text1.Text = "" Then Exit Sub
If IsNumeric(Text1.Text) Then
If txtBoxText <> Text1.Text Then
txtBoxSelStart = Text1.SelStart
Else
Text1.SelStart = txtBoxSelStart
End If
txtBoxText = Text1.Text
Else
Text1.Text = txtBoxText
End If
End SubPrivate Sub Text1_GotFocus()
txtBoxSelStart = Text1.SelStart
txtBoxText = Text1.Text
End SubPrivate Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode >= vbKeyEnd And KeyCode <= vbKeyDown Then
txtBoxSelStart = Text1.SelStart
End If
End SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbKeyLButton Then
txtBoxSelStart = Text1.SelStart
End If
End Sub
Private Sub Text1_Change()
If Text1.Text = "" Then '<-----------------
txtBoxText = "" '<-----------------
txtBoxSelStart = 0 '<-----------------
Exit Sub '<-----------------
End If '<-----------------
If IsNumeric(Text1.Text) Then
If txtBoxText <> Text1.Text Then
txtBoxSelStart = Text1.SelStart
Else
Text1.SelStart = txtBoxSelStart
End If
txtBoxText = Text1.Text
Else
Text1.Text = txtBoxText
End If
End Sub
'===============================================
'窗体中
'===============================================
Private Sub Form_Load()
Hook
End SubPrivate Sub Form_Unload(Cancel As Integer)
UnHook
End Sub
'===============================================
'模块中
'===============================================
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Private Type PKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End TypePrivate Const HC_ACTION = 0
Private Const WM_KEYDOWN = &H100
Private Const WM_SYSKEYDOWN = &H104
Private Const WM_KEYUP = &H101
Private Const WM_SYSKEYUP = &H105
Private Const VK_LWIN = &H5B
Private Const VK_RWIN = &H5C
Private Const VK_APPS = &H5D
Private Const WH_KEYBOARD_LL = 13Private PrevHook As Long
Public Function LowLevelKeyboardProc(ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim fEatKeystroke As Boolean
Dim p As PKBDLLHOOKSTRUCT
If ncode = HC_ACTION Then
Select Case wParam
Case WM_KEYDOWN, WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYUP
CopyMemory p, ByVal lParam, Len(p)
'If p.vkCode = VK_LWIN Or p.vkCode = VK_RWIN Then '左右Win键
If p.vkCode = VK_APPS Then
fEatKeystroke = True
End If
Case Else
'do nothing
End Select
End If
If fEatKeystroke Then
LowLevelKeyboardProc = 1
Else
CallNextHookEx WH_KEYBOARD_LL, ncode, wParam, lParam
End If
End Function
Public Sub Hook()
PrevHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0)
End Sub
Public Sub UnHook()
UnhookWindowsHookEx PrevHook
End Sub
on error goto xx
如果不是数字,那么在计算中就会产生"类型不匹配"的错误,当然文本框的变量类型要设置正确!
xx语句就给个提示,比如说:输入有误之类的