Results 1 to 4 of 4

Thread: [RESOLVED] Disable Ctrl+C, Ctrl+X, Ctrl+V in textbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    24

    Resolved [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.

  2. #2
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    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.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    24

    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

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width