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


i have textbox component in c# winforms.

when right-click on it, disable paste option, whilst still allowing users cut , copy.

i have investigated few options, neither of meet requirements -

  1. if use option, prevent cut , copy along paste not want.

    txt.shortcutsenabled = false;

  2. if override contextmenu of textbox, have write cut , copy features myself in new context menu.

    txt.contextmenu = new contextmenu(); // or other

are there options can use disable paste option of default context menu, retaining cut , copy?

assuming paste menu item fifth element in textbox context menu (zero-based , separator counts item too), subclass textbox class (here: custommenutextbox) , override wndproc method disable 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 , mf_grayed             user32.enablemenuitem(menuhandle, 4, 0x00000400 | 0x00000001);         }          base.wndproc(ref m);     } } 

based on add item default textbox context menu.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -