How to Disable Copy, Paste and Delete Features on a Textbox Using C#

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.

TextBox : Disable the 'Paste' option whilst allowing 'Cut' and 'Copy' on Right-click

Assuming the Paste menu item is always the fifth element in the textbox context menu (zero-based and a separator counts as item too), you could subclass the TextBox class (here: CustomMenuTextBox) and override the WndProc method to disable that specific menu item:

public static class User32
{
[DllImport("user32.dll")]
public static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
}

public class CustomMenuTextBox : TextBox
{
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0093 /*WM_UAHINITMENU*/ || m.Msg == 0x0117 /*WM_INITMENUPOPUP*/ || m.Msg == 0x0116 /*WM_INITMENU*/)
{
IntPtr menuHandle = m.Msg == 0x0093 ? Marshal.ReadIntPtr(m.LParam) : m.WParam;

// MF_BYPOSITION and MF_GRAYED
User32.EnableMenuItem(menuHandle, 4, 0x00000400 | 0x00000001);
}

base.WndProc(ref m);
}
}

Based on Add item to the default TextBox context menu.

Disable paste on a textbox via keypress

Those properties aren't available in the KeyPress event:

The KeyPress event is not raised by non-character keys other than space and backspace; however, the non-character keys do raise the KeyDown and KeyUp events.

Subscribe to the KeyDown event, where you have access to any modifier keys (control, alt, shift) the user happens to be pressing.

 private void textBox18_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
{
// cancel the "paste" function
e.SuppressKeyPress = true;
}
}

How to suppress Cut, Copy and Paste Operations in TextBox in WPF?

You can do this pretty easily using the CommandManager.PreviewCanExecute routed event. In your XAML, you would put the following on your TextBox element. This will apply to CTL+V, etc as well as the context menu or any buttons that you may have mapped to those commands so it's very effective.

<TextBox CommandManager.PreviewCanExecute="HandleCanExecute" />

Then in your code-behind, add a HandleCanExecute method that disables the commands.

private void HandleCanExecute(object sender, CanExecuteRoutedEventArgs e) {

if ( e.Command == ApplicationCommands.Cut ||
e.Command == ApplicationCommands.Copy ||
e.Command == ApplicationCommands.Paste ) {

e.CanExecute = false;
e.Handled = true;

}

}

Disable copy & paste content from outside source of WPF installer

Here is the answer. We need to clear the clipboard when deactivate the window and can be set the text again into clipboard while activate.

private string oldClipboardContent { get; set; } = "";

private void Window_Activated(object sender, EventArgs e)
{
Clipboard.SetText(oldClipboardContent);
}

private void Window_Deactivated(object sender, EventArgs e)
{
oldClipboardContent = Clipboard.GetText();
Clipboard.Clear();
}

Disable Copy or Paste action for text box?

Check this fiddle.

 $('#email').bind("cut copy paste",function(e) {
e.preventDefault();
});

You need to bind what should be done on cut, copy and paste. You prevent default behavior of the action.

You can find a detailed explanation here.

How to prevent richTextBox to paste images within it?

I used this code and it works perfectly:

private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//if ((Control.ModifierKeys & Keys.Control) == Keys.Control && (e.KeyChar == 'V' || e.KeyChar == 'v'))
if (((int)e.KeyChar) == 22)
{
if(hasImage(Clipboard.GetDataObject()));
{
e.Handled = true;
richTextBox1.Undo();
MessageBox.Show("can't paste image here!!");
}

}
}

private Boolean hasImage(object obj)
{
System.Windows.Forms.RichTextBox richTextBox = new System.Windows.Forms.RichTextBox();
Object data = Clipboard.GetDataObject();
Clipboard.SetDataObject(data);
richTextBox.Paste();
Clipboard.SetDataObject(data);
int offset = richTextBox.Rtf.IndexOf(@"\f0\fs17") + 8; // offset = 118;
int len = richTextBox.Rtf.LastIndexOf(@"\par") - offset;
return richTextBox.Rtf.Substring(offset, len).Trim().Contains(@"{\pict\");
}

And disabled dragdrop in richTextBox.

thanks

How to disable textbox from editing?

You can set the ReadOnly property to true.

Quoth the link:

When this property is set to true, the contents of the control cannot
be changed by the user at runtime. With this property set to true, you
can still set the value of the Text property in code. You can use this
feature instead of disabling the control with the Enabled property to
allow the contents to be copied and ToolTips to be shown.



Related Topics



Leave a reply



Submit