Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I disable pasting into a TextBox?

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



Page view counter