Can anybody please suggest how to handle Cut,Copy and Paste events on a Text Box in WinForms using C#?
-
1TextBox already handles this itself, why do you want to help? – Hans Passant Feb 25 '11 at 04:47
-
@Sharp: be clear. Do you want to disable those features, or handle them yourself? – p.campbell Feb 25 '11 at 05:05
-
2Make your textbox property readonly to true... – Crimsonland Feb 25 '11 at 05:51
-
@p.Campbell: First of all thanks a lot for your comments on my question and sorry for late response but I clearly mentioned in the title that I want to disable all the Cut,Copy and Paste functionality in my Win Form TextBox. – SharpUrBrain Feb 25 '11 at 10:30
-
@Crimsonland: If I am making my textbox.readonly=true then I can not edit it again and I want it as an editable textbox. Thanks for your comment – SharpUrBrain Feb 25 '11 at 10:33
-
@Sharp: Your title said 'disable', and your question only said 'handle'. I don't know where my confusion came from. Protip: if 2+ answerers (Hans and me) both didn't immediately understand, then your question wasn't clear. I was trying to *help you* by asking you to be as clear as possible. Yes, you clearly said in the title 'disabling', but then didn't mention anything about 'disabling' once in your question. 'Handle' typically would mean override and implement yourself. – p.campbell Feb 25 '11 at 14:52
-
What do you mean by the "cursor goes to first position"? The cursor should move to the *end* of the pasted text. This is the standard behavior; you'll see it in every other application on your computer. You need to do a *lot* better job explaining exactly what the different things you want your application to do are. – Cody Gray - on strike Feb 26 '11 at 04:55
-
woah, woah woah! you changed the entire body and title of the question and then answered and awarded it to yourself? Someone should flag this. – bryanbcook Apr 14 '11 at 01:44
-
@bryan: I sympathize, but the first priority is good content, and the original question sucked. – Robert Harvey Apr 14 '11 at 02:19
-
@bryan: First I was thinking to handle the Cut, copy and paste in my textbox to fix my cursor position and to avoid extra spaces at beginning and at end, but again I did not find all the key combination for cut,copy and Paste events. So instead of making disable it or handling it I trimmed the extra spaces from beginning and at the end of the text pasted in my textbox. Hence I changed the question title and body but still it is somewhat same not totally different. Sorry I did not describe complete scenario but believe me I did not do it intentionally. – SharpUrBrain Apr 14 '11 at 04:39
6 Answers
In WinForms, the easiest way to disable cut, copy and paste features on a textbox is to set the ShortcutsEnabled property to false.
-
2
-
8For those, who wants to know an answer if the ShortcutsEnabled property set to false prevents user to use right click and paste - Yes, it prevents. – jurajvt Jul 18 '16 at 14:48
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:
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);
}
}

- 239,200
- 50
- 490
- 574

- 16,210
- 2
- 40
- 69
-
1This was the solution for me. It is especially a useful approach when needed for a textbox to enter a password: in that case you would want to allow WM_PASTE, but not the others. – mike Aug 05 '15 at 08:54
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);
}
}

- 98,673
- 67
- 256
- 322
-
Why would you suggest handling this at the form level, rather than subclassing the `TextBox` control itself? – Cody Gray - on strike Feb 25 '11 at 06:07
-
6@Cody: it's a simple demo. The question contained absolutely no info on the scale of the feature, the app, the use-cases, etc. So questions around 'why strategy X and not strategy Y' are tough to ask and answer when no real info is given by the questioner. – p.campbell Feb 25 '11 at 06:47
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().
if (copyPasteEnabled) {
textBox1.ShortcutsEnabled = true;
textBox1.ContextMenu = null;
} else {
textBox1.ShortcutsEnabled = false;
textBox1.ContextMenu = new ContextMenu();
}

- 1,890
- 19
- 26
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
}
if (e.Control == true)
{
switch (e.KeyCode)
{
case Keys.C:
case Keys.P:
case Keys.X:
e.Handled = true;
textBox1.SelectionLength = 0;
break;
}
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
System.Windows.Forms.Clipboard.Clear();
}

- 54,264
- 27
- 148
- 161

- 17
- 1
-
This answer is terrible because program must never clear clipboard or put content on it without user interaction. – Igor Levicki Jul 15 '20 at 09:39
int cusorposition = m_TextBox1.SelectionStart;
if (TextBox1.Text[0] == ' ')
{
//Trim Spaces at beginning.
m_TextBox1.Text = m_TextBox1.Text.TrimStart(' ');
m_TextBox1.Text = m_TextBox1.Text.TrimEnd(' ');
m_TextBox1.SelectionStart = cusorposition ;
}
Hi I found a way how to get the current cursor position instead of handling cut, copy and Paste event in a text box named TextBox1.Here in the above I am keeping the backup of current Cursor Position and after trimming the extra spaces from the starting and from end position I am reassigning the current cursor position.
Thanks to all who helped me to fix this problem.

- 37,735
- 14
- 62
- 96

- 3,180
- 5
- 38
- 56