How do I disable pasting into a TextBox (via Ctrl + V and the context menu)?

Platform: WinForms| Category: TextBox

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:


		// In 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);
		}

		’ In VB.Net
		Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
			If keyData =(Keys.Control | Keys.V) Then
				Return True
			Else 
				Return MyBase.ProcessCmdKey( msg, keyData)
			End If
		End Function

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.