how to disable copy, Paste and delete features on a textbox using C#

In WinForms, the easiest way to disable cut, copy and paste features on a textbox is to set the ShortcutsEnabled property to false.


You'd have to subclass the textbox and then override the WndProc method to intercept the windows messages before the control does.

Here's an example that illustrates a TextBox that intercepts the WM_PASTE message.

And for reference, here's the definition of the message constants:

  • WM_PASTE
  • WM_COPY
  • WM_CUT

You'd simply ignore the inbound message, like so:

protected override void WndProc(ref Message m)
{
   if (m.Msg == WM_PASTE || m.Msg == WM_COPY || m.Msg == WM_CUT)
   {
      // ignore input if it was from a keyboard shortcut
      // or a Menu command
   }
   else
   {
      // handle the windows message normally
      base.WndProc(ref m);
   }
}

Suppose you have a TextBox named textbox1. It sounds like you want to disable the cut, copy and paste functionality of a TextBox.

Try this quick and dirty proof of concept snippet:

private void Form1_Load(object sender, EventArgs e)
{
    ContextMenu _blankContextMenu = new ContextMenu();
    textBox1.ContextMenu = _blankContextMenu; 
}


private const Keys CopyKeys = Keys.Control | Keys.C;
private const Keys PasteKeys = Keys.Control | Keys.V;

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if ((keyData == CopyKeys) || (keyData == PasteKeys))
    {
        return true;
    }
    else
    {
        return base.ProcessCmdKey(ref msg, keyData);
    }
}