Results 1 to 11 of 11

Thread: prevent paste into textbox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    525

    prevent paste into textbox

    any ideas how I could prevent someone from pasting text into a textbox?

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Use .Locked = True, or on the OnClick event, clear the clipboard. But .Locked will not let you type, and OnClick will clear images and stuff too.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3
    Hyperactive Member
    Join Date
    Dec 2000
    Posts
    462

    Text

    I Ran Across this routine somewhere and I will turn of all pasteing and copying in a text box.

    But as you will find out it has its limits.

    VB Code:
    1. Public Declare Function GetWindowLong Lib "user32" Alias _
    2.    "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    3.    Public Declare Function SetWindowLong Lib "user32" Alias _
    4.    "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As _
    5.    Long, ByVal dwNewLong As Long) As Long
    6.    Public Declare Function CallWindowProc Lib "user32" Alias _
    7.    "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd _
    8.    As Long, ByVal Msg As Long, ByVal wParam As Long, _
    9.    ByVal lParam As Long) As Long
    10.    Public Const GWL_WNDPROC = (-4)
    11.    Public Const WM_RBUTTONUP = &H205
    12.    Public Const WM_NCDESTROY = &H82
    13.    Public Function WndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    14.     On Error Resume Next
    15.     Dim txtcontrol As Object
    16.     Set txtcontrol = GetControlFromhWnd(hWnd)
    17.     If TypeOf txtcontrol Is TextBox Then
    18.      If Msg <> WM_RBUTTONUP Then
    19.       WndProc = CallWindowProc(CLng(txtcontrol.Tag), hWnd, Msg, wParam, lParam)
    20.      Else
    21.       'Put custom menu here
    22.       'eg.
    23.       'form1.PopUpMenu form1.mnupopup
    24.       WndProc = 0
    25.      End If
    26.     If Msg = WM_NCDESTROY Then UnHookWindow (txtcontrol)
    27.    Else
    28.     'oops
    29.     WndProc = 0
    30.    End If
    31.    End Function
    32.    Public Function GetControlFromhWnd(hWnd As Long) As Object
    33.    'Parameters: hWnd - Window Handle to object
    34.    'Returns: Object reference to cotrol with the hwnd
    35.    'Note this will not return forms
    36.    Dim frm As Form
    37.    Dim ctl As Control
    38.    For Each frm In Forms
    39.    For Each ctl In frm.Controls
    40.     If ctl.hWnd = hWnd Then
    41.     Set GetControlFromhWnd = ctl
    42.     Exit Function
    43.     End If
    44.    Next
    45.    Next
    46.   ' Beep
    47.    End Function
    48.    Public Sub HookWindow(hookobject As TextBox)
    49.     If hookobject.Tag <> "" Then Exit Sub
    50.     'remember old window handler
    51.     hookobject.Tag = CStr(GetWindowLong(hookobject.hWnd, GWL_WNDPROC))
    52.     'install old window handler
    53.     Call SetWindowLong(hookobject.hWnd, GWL_WNDPROC, AddressOf WndProc)
    54.    End Sub
    55.    Public Sub UnHookWindow(hookobject As TextBox)
    56.     'if no old window handler remembered - nothing to replace current one with
    57.     If hookobject.Tag = "" Then Exit Sub
    58.     'replace ole window handler
    59.     Call SetWindowLong(hookobject.hWnd, GWL_WNDPROC, CLng(hookobject.Tag))
    60.     hookobject.Tag = ""
    61.    End Sub

    Put all of This in too a mobule.
    Don
    (OLD DOS Programmer)

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Dbee, your code is a bit mashed together, so I honestly can't tell if this is the same, but...
    VB Code:
    1. 'In a form
    2. Option Explicit
    3.  
    4. Private Sub Form_Load()
    5.     Call Hook(Text1.hWnd)
    6. End Sub
    7.  
    8. Private Sub Form_Unload(Cancel As Integer)
    9.     Call Unhook(Text1.hWnd)
    10. End Sub
    11.  
    12.  
    13. 'In a module
    14. Option Explicit
    15.  
    16. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, _
    17.                                                                             ByVal nIndex As Long, _
    18.                                                                             ByVal dwNewLong As Long) _
    19.                                                                             As Long
    20. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
    21.                                                                               ByVal hWnd As Long, _
    22.                                                                               ByVal Msg As Long, _
    23.                                                                               ByVal wParam As Long, _
    24.                                                                               ByVal lParam As Long) _
    25.                                                                               As Long
    26. Private Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hWnd As Long, _
    27.                                                                             ByVal wMsg As Long, _
    28.                                                                             ByVal wParam As Long, _
    29.                                                                             ByVal lParam As Long) _
    30.                                                                             As Long
    31. Private Const GWL_WNDPROC = (-4)
    32. Private Const WM_PASTE = &H302
    33.  
    34. Private mPrevProc As Long
    35.  
    36. Public Function TextWndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    37. On Error Resume Next
    38.  
    39.     If uMsg = WM_PASTE Then
    40.         TextWndProc = 0&
    41.         Exit Function
    42.     End If
    43.  
    44.     If mPrevProc > 0 Then
    45.         TextWndProc = CallWindowProc(mPrevProc, hWnd, uMsg, wParam, lParam)
    46.     Else
    47.         TextWndProc = DefWindowProc(hWnd, uMsg, wParam, lParam)
    48.     End If
    49.  
    50. End Function
    51.  
    52. Public Sub Hook(hWnd As Long)
    53.     mPrevProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf TextWndProc)
    54. End Sub
    55.  
    56. Public Sub Unhook(hWnd As Long)
    57.    
    58.     Call SetWindowLong(hWnd, GWL_WNDPROC, mPrevProc)
    59.     mPrevProc = 0&
    60.    
    61. End Sub

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    Hyperactive Member
    Join Date
    Dec 2000
    Posts
    462

    Hook

    I used hook textbox as a call for the textbox I was using and you will have to call it for every one you use.

    An yes the code is to compacked sorry for that your last post is correct except you call it for the text boxes only. not the entire form.

    The trouble I had with it if i had more than one form loaded then the form that lost focus would go blank on me. Screwed up the non focus forms.

    If you are using one form only then it works like a charm.
    Don
    (OLD DOS Programmer)

  6. #6
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    On the keypress event:

    if keyascii = 22 then
    'ctrl+v pressed
    keyascii = 0
    end if

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by DiGiTaIErRoR
    On the keypress event:

    if keyascii = 22 then
    'ctrl+v pressed
    keyascii = 0
    end if
    What about pasting from the context menu?
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    In a RTB you can turn off the autoverb menu.

    Or subclass a textbox and intercept the mousedown event.

    You'll have fun with that.

  9. #9
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Or you can use the code a few posts up.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  10. #10
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Originally posted by crptcblade
    Or you can use the code a few posts up.

    But if he wants a simple solution, and is using a RTB, my code would work best.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    525
    awesome, thanks for you help!

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