How do I prevent the user from being able to paste from the clipboard into a TextBox, e.g., through Ctrl+V and the context menu?
First set the ContextMenu property of the TextBox to a dummy, empty ContextMenu instance. This will prevent the default context menu from showing. Then, override the ProcessCmdKey method as follows in a TextBox derived class:
[C#]
protected override bool ProcessCmdKey( ref Message msg, Keys keyData )
{
if ( keyData == (Keys.Control | Keys.V) )
return true;
else
return base.ProcessCmdKey( ref msg, keyData );
}
[Visual Basic]
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, _
ByVal keyData As Keys) As Boolean
If keyData = (Keys.Control Or Keys.V) Then
Return True
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function
Contributed from George Shepherd's Windows Forms FAQ