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 -
if use option, prevent cut , copy along paste not want.
txt.shortcutsenabled = false;
if override
contextmenu
oftextbox
, 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
Post a Comment