Prevent to copy & paste in masked edit text-box in C#/vb.net
How to prevent to copy & paste any text [usi开发者_开发百科ng right click and also Ctrl+C and Ctrl+v keys] in masked edit text-box in C#/vb.net?
You could do it by replacing the default context menu with your own context menu to disable mouse clicks and then you could just stop any keyboard input while the Control key is pressed by handling the KeyDown event and writing code like:
If e.Modifiers = Keys.Control Then
e.Handled = True
End If
Inherit from TextBox
and override the WndProc
method :
private const int WM_CUT = 0x0300;
private const int WM_COPY = 0x0301;
private const int WM_PASTE = 0x0302;
protected override void WndProc(ref Messsage m)
{
if (m.Msg == WM_CUT || m.Msg == WM_COPY || m.Msg == WM_PASTE)
return;
base.WndProc(ref m);
}
It should also prevent copy/paste from the context menu
for ctrl+v/c You need to inherit the TextBox class and override the function ProcessCmdKey
and then call base class only if ctrl+c or ctrl+v are not pressed.
Set ShortcutsEnabled property of textbox to False. Drawbacks: This will also prevent Ctrl+A and Right Click Menu of that textbox.
精彩评论