-
Aug 9th, 2011, 07:19 AM
#1
Thread Starter
Junior Member
[RESOLVED] Disable Ctrl+C, Ctrl+X, Ctrl+V in textbox
Hi,
How can I disable right-click and shortcut keys (e.g Ctrl+C, Ctrl+X, Ctrl+V) in the TextBox?
Thank you very mach
Last edited by IranVB; Aug 9th, 2011 at 08:32 AM.
-
Aug 9th, 2011, 08:06 AM
#2
Re: Disable Ctrl+C, Ctrl+X, Ctrl+V in textbox
To disable right click
Module code
Code:
Option Explicit
Public OldWindowProc As Long
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
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public Const WM_CONTEXTMENU = &H7B
' *********************************************
' Pass along all messages except the one that
' makes the context menu appear.
' *********************************************
Public Function NewWindowProc(ByVal hWnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If msg <> WM_CONTEXTMENU Then _
NewWindowProc = CallWindowProc( _
OldWindowProc, hWnd, msg, wParam, _
lParam)
End Function
Form code
Code:
Private Sub Form_Load()
OldWindowProc = SetWindowLong(Text1.hWnd, GWL_WNDPROC, AddressOf NewWindowProc)
End Sub
To disable keys, use keypress or keydown event of textbox
Last edited by seenu_1st; Aug 9th, 2011 at 08:12 AM.
-
Aug 9th, 2011, 08:23 AM
#3
Thread Starter
Junior Member
Re: Disable Ctrl+C, Ctrl+X, Ctrl+V in textbox
oh thank you, code was very useful
To disable keys, I found useful codes in the below topic:
http://www.vbforums.com/showthread.php?t=539314
good luck
-
Aug 9th, 2011, 08:30 AM
#4
Re: Disable Ctrl+C, Ctrl+X, Ctrl+V in textbox
Though the code by seenu_1st works, due to the dangers of subclassing especially while in IDE, recommend not subclassing in Form_Load, but on & off as needed...
Code:
Private Sub Text1_LostFocus()
If OldWindowProc Then
SetWindowLong Text1.hWnd, GWL_WNDPROC, OldWindowProc
OldWindowProc = 0&
End If
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
If OldWindowProc = 0 Then OldWindowProc = SetWindowLong(Text1.hWnd, GWL_WNDPROC, AddressOf NewWindowProc)
End If
End Sub
Also note that the method provided only works for 1 textbox. Will need to modify method to subclass multiple textboxes, if needed.
Edited: Subclassing the textbox at form_load will catch all instances of context messages that can occur. Subclassing on demand as shown above may not catch them all but is safer to use until ready to compile. Here are some cases where subclassing on demand may not catch context menus
a. User presses Shift+F10 (can be trapped in keydown event)
b. User presses Apps Key (key between right ctrl & right alt). Cannot be trapped in KeyDown event
c. Something other than your code sends your textbox commands that would display the menu
Last edited by LaVolpe; Aug 9th, 2011 at 08:41 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|