locked
Disable copy-paste in textbox RRS feed

  • Question

  • Hi,

    How do I disable that the user can't paste data into a textbox, and how do I disable the contextmenustrip of the textbox the one that always appears with the copy-paste options two.

    Tuesday, September 19, 2006 4:07 PM

Answers

  • Hi, Amendez,

      Select the textbox in the designer, and in the property grid, set "ReadOnly" to True.  This will disallow any typing/pasting into the control, and will grey out the appropriate entires from the context menu.

    If, for some reason, you still want the user to type into the textbox (but just not paste into it) and want the cut/copy/paste entries in the menu to go away completely, you need to set "ShortcutsEnabled" to False for the textbox control, and provide your own ContextMenuStrip (which you can drag over from the toolbox into the form designer), selecting it in the "ContextMenuStrip" value in the property grid for the TextBox control -- that will override the default context menu.

     

    Hope this helps,

      --Matt--*

    Tuesday, September 19, 2006 10:52 PM
    Moderator

All replies

  • Hi, Amendez,

      Select the textbox in the designer, and in the property grid, set "ReadOnly" to True.  This will disallow any typing/pasting into the control, and will grey out the appropriate entires from the context menu.

    If, for some reason, you still want the user to type into the textbox (but just not paste into it) and want the cut/copy/paste entries in the menu to go away completely, you need to set "ShortcutsEnabled" to False for the textbox control, and provide your own ContextMenuStrip (which you can drag over from the toolbox into the form designer), selecting it in the "ContextMenuStrip" value in the property grid for the TextBox control -- that will override the default context menu.

     

    Hope this helps,

      --Matt--*

    Tuesday, September 19, 2006 10:52 PM
    Moderator
  • Hello friend,

    If you are developing in .net it's pretty easy.
    -first if you don't want the popup menu to appear, then replace the contextMenu of the control with a blank menu of your own. To do this, place a context menu on your form and rename it say to myContextMnu
    Do no add anything to this menu. Then specify this menu as the ContextMenu property of the textbox.
    When the user right clicks in your textbox nothing will happen.

    -Second, You can also disable copy and paste when user attempts to do it using Ctrl+C and Ctrl+V.
    to do this,Create a KeyDown Event handler for the text box and then paste this code:

    If e.Modifiers = Keys.Control Then

    e.Handled = True

    MyTextBox.SelectionLength = 0

    End If

    In french we say: "Et voila"

    Yours Trully,
    BertrandKis

    Friday, October 6, 2006 9:24 AM
  • for VB6:

    Private Sub txtPassw_KeyDown(KeyCode As Integer, Shift As Integer)
        If Shift = vbCtrlMask And (Chr(KeyCode) = "v" Or Chr(KeyCode) = "V") Then
            txtPassw.Locked = True
        Else
            txtPassw.Locked = False
        End If
    End Sub


    Private Sub txtPassw_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbRightButton Then
            txtPassw.Locked = True
        Else
            txtPassw.Locked = False
        End If
    End Sub

    Tuesday, January 30, 2007 3:34 PM
  • jst want to say thankx...

    this code was of a gr8 help for me....!

    Tuesday, October 23, 2007 6:56 AM
  • Simply through alert message.

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script language="javascript">
    function whichButton(event)
    {
    if (event.button==2)//RIGHT CLICK
      {
      alert("Not Allow Right Click!");
      }

    }
    function noCTRL(e)
    {
    var code = (document.all) ? event.keyCode:e.which;

    var msg = "Sorry, this functionality is disabled.";
    if (parseInt(code)==17) //CTRL
    {
    alert(msg);
    window.event.returnValue = false;
    }
    }
    </script>
    </head>
    <body>
    <form method="">
    <strong>Not Allow Paste </strong><BR>
    <input type="text" value="" onMouseDown="whichButton(event)"  onKeyDown="return noCTRL(event)"/>
    </form>
    </body>
    </html>

    Tuesday, March 18, 2008 6:07 PM
  • Thanks,

     

    I did not know that that such an option exsisted. I'ts like an instant copyright for my contents.

     

    Regards, patrick1128

    Saturday, April 19, 2008 1:10 AM
  • Matt,

    Thanks That helped me...

    Rakesh
    Thanks
    • Proposed as answer by pu239u235 Wednesday, January 27, 2010 8:27 PM
    Friday, May 30, 2008 7:20 PM
  • In VB 2008 Express, the following is what I found to work.

    In the keydown event for any given textbox, I used the following method.  It is similar to one method mentioned above but slightly modified.

    If

     

    ModifierKeys = Keys.Control Then

    e.Handled =

    True

    TextBox.ReadOnly =

    True

     

    Else

    TextBox.ReadOnly =

    False

     

    End If

    --------

    for the click event of the same textbox I used the following code.

    TextBox3.ReadOnly =

    False

     

     

    So far it works as I intended it too.  To keep users from pasting into the textbox.

    Wednesday, January 27, 2010 8:31 PM
  • if it is window application then go to the Enter event of the textbox and  put following code

    Clipboard.Clear()

    Monday, May 28, 2012 10:41 AM