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 have a Delete key shortcut on a form's main menu, and still process the Delete key in a TextBox when that control has the focus?

I have a delete key shortcut for my main menu. When my textbox is being edited, pressing delete activates this menu items instead of being processed by the TextBox. How can I get my TextBox to handle this delete?

Subclass the TextBox and override its PreProcessMessage method.

public class CustomTextBox : TextBox
{
  const int WM_KEYDOWN = 0x100;
  const int WM_KEYUP = 0x101;

  public override bool PreProcessMessage( ref Message msg )
  {
    Keys keyCode = (Keys) (int) msg.WParam & Keys.KeyCode;
    if ( (msg.Msg == WM_KEYDOWN || msg.Msg == WM_KEYUP)
      && keyCode == Keys.Delete )
      return false;
    return base.PreProcessMessage( ref msg );
  }
}

Contributed from George Shepherd's Windows Forms FAQ



Page view counter