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 prevent the ContextMenu from being displayed on certain keyboard keys such as Keys.Apps?

Override WndProc in your Control and do the following. You should then listen to keyup and show the context menu yourself.

[C#]

private int const WM_CONTEXTMENU = 0x7b;
private int const WM_KEYUP = 0x101;

protected override void WndProc(ref Message m)
{
  if ( m.Msg == WM_CONTEXTMENU ) return;
  if ( m.Msg == WM_KEYUP )
  {
    Keys keys = (Keys) m.WParam.ToInt32();
    // Prevent this key from being processed.
    if ( keys == Keys.Apps ) return;
  }
  base.WndProc( ref m );
}

[Visual Basic]

Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = 0x7b Then 'WM_CONTEXTMENU
Return
End If
If m.Msg = 0x101 Then 'WM_KEYUP
Dim keys As Keys = CType(m.WParam.ToInt32(), Keys)
' Prevent this key from being processed.
If keys = Keys.Apps Then
Return
End If
End If
MyBase.WndProc( m)
End Sub

Contributed from George Shepherd's Windows Forms FAQ