Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 646905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:39:52+00:00 2026-05-13T21:39:52+00:00

I wonder what the Form.KeyPreview property actually is good for? Why do it exist

  • 0

I wonder what the Form.KeyPreview property actually is good for? Why do it exist and what do I “risk” by setting it to true? I guess it must have some negative effect – otherwise it shouldn’t exist at all (or at least be true by default)?

EDIT: I know perfectly well what it does. I’m asking why. Why do I have to set it to true in order to make the keyboard events fire? Why doesn’t the keyboard events always fire for a form. What isn’t just that the standard behavior?

The particular reason I ask is: I have just set KeyPreview = true in the base form of my app, which all other forms inherit from. Am I in for any nasty surprise?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:39:52+00:00Added an answer on May 13, 2026 at 9:39 pm

    Form.KeyPreview is a bit of an anachronism, inherited from the Visual Basic object model for form design. Back in the VB6 days, you needed KeyPreview to be able to implement short-cut keystrokes. That isn’t needed anymore in Windows Forms, overriding the ProcessCmdKey() is the better solution:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
      if (keyData == (Keys.Control | Keys.F)) {
        DoSomething();   // Implement the Ctrl+F short-cut keystroke
        return true;     // This keystroke was handled, don't pass to the control with the focus
      }
      return base.ProcessCmdKey(ref msg, keyData);
    }
    

    But KeyPreview was supported to help the legion of VB6 programmers switch to .NET back in the early 2000’s. The point of KeyPreview or ProcessCmdKey() is to allow your UI to respond to shortcut keystrokes. Keyboard messages are normally sent to the control that has the focus. The Windows Forms message loop allows code to have a peek at that message before the control sees it. That’s important for short-cut keys, implementing the KeyDown event for every control that might get the focus to detect them is very impractical.

    Setting KeyPreview to True doesn’t cause problems. The form’s KeyDown event will run, it will only have an affect if it has code that does something with the keystroke. But do beware that it closely follows the VB6 usage, you can’t see the kind of keystrokes that are used for navigation. Like the cursor keys and Tab, Escape and Enter for a dialog. Not a problem with ProcessCmdKey().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can create a form with Django that has a mysql background. I wonder
I wonder if anyone can help me out with this.. I have a foreach
I wonder if it is a good idea (and in a quirky funny way)
I have a form in my iPhone application which contains three textFields and a
I have a form I am creating and I would like to send information,
I wonder why asp.net doesn't allow this? Lot's of pages contain more than 1
I wonder why this code never runs when I release a key. private void
I created a form in WPF and it looked great. When I opened it
I am trying to grab contents/tags inside form tag using preg_match_all, here is the
In prototype I could post a form to its action with its populated form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.