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 306591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:28:52+00:00 2026-05-12T07:28:52+00:00

Using winforms, I have set the KeyPreview property to true and have event handles

  • 0

Using winforms, I have set the KeyPreview property to true and have event handles for the proper key events within the base form as well.

Within the forms that inherit from it, I set the AcceptButton property based on the requirements of the application.

There are certain cases in which I want the enter key to have functionality different than that of the AcceptButton.

I was hoping to capture the enter key press within my base form and check for the special cases where I do not want the AcceptButton event to fire.

It appears though, that the AcceptButton click is fired before any of the key events within my basef form. I could write functionality into the click events of the possible acceptbuttons, but, in my opinion, that would be a hack.

Any suggestions?

Thanks.

  • 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-12T07:28:52+00:00Added an answer on May 12, 2026 at 7:28 am

    Another way to handle this is to override the form’s ProcessDialogKey() method where you can suppress the accept and/or cancel buttons. For example, I have an application with a filter editor that filters a grid based on user input. I want the user to be able to hit the return key when the filter editor control has the focus to apply the filter. The problem is the accept button code runs and closes the form. The code below resolves the issue.

        protected override bool ProcessDialogKey(Keys keyData)
        {
            // Suppress the accept button when the filter editor has the focus.
            // This doesn't work in the KeyDown or KeyPress events.
            if (((keyData & Keys.Return) == Keys.Return) && (filterEditor.ContainsFocus))
                return false;
    
            return base.ProcessDialogKey(keyData);
        }
    

    You can take this even further by dropping the following code in a base dialog form. Then you can suppress the accept button for controls in subclasses as necessary.

        private readonly List<Control> _disableAcceptButtonList = new List<Control>();
    
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (((keyData & Keys.Return) == Keys.Return) && (_disableAcceptButtonList.Count > 0))
            {
                foreach (Control control in _disableAcceptButtonList)
                    if (control.ContainsFocus)
                        return false;
            }
    
            return base.ProcessDialogKey(keyData);
        }
    
        protected virtual void DisableAcceptButtonForControl(Control control)
        {
            if (!_disableAcceptButtonList.Contains(control))
                _disableAcceptButtonList.Add(control);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 152k
  • Answers 152k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you are building your Repository to be specific to… May 12, 2026 at 10:12 am
  • Editorial Team
    Editorial Team added an answer In JavaScript you can get the current date and time… May 12, 2026 at 10:12 am
  • Editorial Team
    Editorial Team added an answer My first worry is: When "getting" a certain version, can… May 12, 2026 at 10:12 am

Related Questions

I just migrated to Windows Ultimate 64 SP1, because my system's hard drive crashed.
I'm using a winforms webbrowser control to display some content in a windows forms
In my C# winforms app, I have a datagrid. When the datagrid reloads, I
I have a L2S SalesOrder entity with a child set of LineItems. Using winforms,
The WinForms ProgressBar control allows its foreground color to be set. While this normally

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.