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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:17:47+00:00 2026-05-31T23:17:47+00:00

I have a winform form which has typical OK and Cancel buttons. The OK

  • 0

I have a winform form which has typical OK and Cancel buttons.

The OK button is set as the default button. When the user presses the ENTER key, the form is closed.

The form also has a text box which has a Search button beside it. The text box allows the user to enter search text and the Search button initiates the search function.

I would like the user to be able to press the ENTER key when the text box has the input focus and have the search function activate.

The problem is that the form is grabbing the ENTER key event before the text box’s handler gets it.

EDIT: I’ve tried using the following event handlers, but they never get hit, and the form closes when the Enter key is hit:

    private void txtFilter_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            Search();

            e.Handled = true;           
        }
    }

    private void txtFilter_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            Search();

            e.Handled = true;
        }
    }

I’ve also tried enabling the KeyPreview property on the form and implementing a KeyDown event handler for the form, but the Enter key never seems to cause any of these to be hit.

What is the best way to accomplish 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-31T23:17:49+00:00Added an answer on May 31, 2026 at 11:17 pm

    Try handling the Enter and Leave events of the TextBox to clear out your form’s AcceptButton property:

    private void txtFilter_Enter(object sender, EventArgs e) {
      this.AcceptButton = null;
    }
    
    private void txtFilter_Leave(object sender, EventArgs e) {
      this.AcceptButton = closeButton;
    }
    

    Then you can just process your KeyUp event as your want:

    private void txtFilter_KeyUp(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Enter)
      {
        Search();         
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MDI WinForms app in which the MDI parent form has a
I have a medium sized WinForm App ( 1 Form that hosts 40 user
I have a winforms app and the main (and only) form has several buttons.
I have a userControl11 (either in winform or wpf) which has a ValueChanged custom
I have WinForm which is a tab based and including all Tabs it has
i've made a C# winforms application. Now i have a form which has lots
I Have a Windows Form Project , which has a main form,and in this
I have a winforms form which is inherited from another form. e.g. class StartForm
I have a WinForm project that contains a form called MainUI. You can see
I have a C# winforms app with a form for user preferences. An admin

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.