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

  • Home
  • SEARCH
  • 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 4060482
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:20:36+00:00 2026-05-20T15:20:36+00:00

How to prevent from closing window with Enter key while the focus in editable

  • 0

How to prevent from closing window with Enter key while the focus in editable control?

Usually, I make Cancel with IsCancel=True and Ok with IsDefault=True. This is because it will allow users to close a dialog with Enter as OK and Esc as Cancel.

However, the problem is pressing Enter key closes the dialog even a keyboard focus is in an editable control such as TextBox.
The best behavior is pressing Enter key closes dialog only when the keyboard focus is not in an editable control. But, pressing Enter key twice should close the dialog. Otherwise, users need to change keyboard focus to another non-editable control to close with Enter key.

So, as a workaround, I implemented this way:

  • Intercepting KeyDown event and check
    if it’s Enter key.
  • If so, check if the keyboard focus is
    at Ok button.
  • If so, closes the dialog with Ok
    button. Otherwise, change the
    keyboard focus to Ok button. So, hit Enter key twice will close the dialog.

This has a problem because the first Enter will change the focus to Ok button, so if the focus is not at the Ok Button, users need to hit Enter twice. This is little different from an ideal behavior. Also, I need to implement this logic to each dialog.

Does someone have a good idea to solve this?

  • 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-20T15:20:37+00:00Added an answer on May 20, 2026 at 3:20 pm

    If looking for this particular behavior I would just simply set some boolean value to true in the KeyDown event handler the first time Enter gets hit (rather than switching the focus). Then, on the next KeyDown event, I’d check if the key hit was Enter and if Enter has been hit before. To implement this in more than one dialog, I would create a generic dialog where I would override the OnKeyDown method rather than subscribing to the event, then subclass that dialog.

    (Having said that, as a user I would not like this behavior at all. I would recommend just ignoring the Enter key altogether when in a text box – that’s what users are used to.)

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

Sidebar

Related Questions

Let's say I've got a control and I want to prevent it from being
How could I prevent esc from closing a dialog box? I searched for this
How do I prevent vim from replacing spaces with tabs when autoindent is on?
How do I prevent code from executing in the SelectedValueChange event of a combobox
I need to prevent user from selecting text (select all or select a portion
i'd like to prevent bots from hacking weak password-protected accounts. (e.g. this happend to
Is there a way to prevent someone from faking a mime type on a
Are there any compatibility issues that would prevent this from working? Do I need
There was a registry fix for Visual Studio 2005 to prevent it from checking
Do you know of a JAXB setting to prevent standalone=yes from being generated in

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.