Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Can anybody please suggest how to handle Cut,Copy and Paste events on a Text Box in WinForms using C#?

like image 395
SharpUrBrain Avatar asked Feb 25 '11 04:02

SharpUrBrain


People also ask

How do I disable paste in TextBox?

Disable Copy and Paste in an ASP.NET Webpage TextBox without JavaScript; we need to set the following properties of the TextBox: oncopy="return false" onpaste="return false" oncut="return false"

How do I restrict copy and paste in C#?

To prevent users to copy/paste using the keyboard set ShortcutsEnabled property to false. To prevent users to copy/paste from the context menu set ContextMenu property to new ContextMenu().

Can you prevent copy and paste?

Preventing copy & paste of text If a user can't select the text, then they can't right-click and copy with the mouse or use Ctrl-C as a keyboard command. Preventing both of these text copying methods is essential to disable copy, and therefore to disable copy paste.


2 Answers

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

like image 96
benPearce Avatar answered Sep 21 '22 02:09

benPearce


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);    } } 
like image 38
bryanbcook Avatar answered Sep 23 '22 02:09

bryanbcook