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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:47:38+00:00 2026-05-11T16:47:38+00:00

Instead of attaching a PreviewKeyUp event with each TextBox in my app and checking

  • 0

Instead of attaching a PreviewKeyUp event with each TextBox in my app and checking if the pressed key was an Enter key and then do an action, I decided to implement extended version of a TextBox that includes a DefaultAction event that fires when an Enter Key is pressed in a TextBox.

What I did was basically create a new Class that extends from TextBox with a public event DefaultAction, like such:

public class DefaultTextBoxControl:TextBox
{
    public event EventHandler<EventArgs> DefaultAction = delegate { };

    public DefaultTextBoxControl()
    {
        PreviewKeyUp += DefaultTextBoxControl_PreviewKeyUp;
    }

    void DefaultTextBoxControl_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.Key != Key.Enter)
        {
            return;
        }
        DefaultAction(this, EventArgs.Empty);
    }
}

I then use this custom textbox from my app like such (xaml):

<Controls:DefaultTextBoxControl  DefaultAction="DefaultTextBoxControl_DefaultAction">
</Controls:DefaultTextBoxControl>

Now in my little experience I’ve had in learning WPF I’ve realized that almost most of the time there is a “cooler” (and hopefully easier) way to implement things

…so my question is, How can I improve the above control? Or maybe is there another way I can do the above control? …maybe using only declarative code instead of both declarative (xaml) and procedural (C#) ?

  • 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-11T16:47:38+00:00Added an answer on May 11, 2026 at 4:47 pm

    Have a look at this blog post from a few months back where I attach a ‘global’ event handler to TextBox.GotFocus to select the text.

    Essentially you can handle the KeyUp event in your App class, like this:

    protected override void OnStartup(StartupEventArgs e)
    {
        EventManager.RegisterClassHandler(typeof(TextBox),
            TextBox.KeyUpEvent,
            new System.Windows.Input.KeyEventHandler(TextBox_KeyUp));
    
        base.OnStartup(e);
    }
    
    private void TextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.Key != System.Windows.Input.Key.Enter) return;
    
        // your event handler here
        e.Handled = true;
        MessageBox.Show("Enter pressed");
    }
    

    … and now every TextBox in your application will call the TextBox_KeyUp method as users type into them.

    Update

    As you’ve pointed out in your comment, this is only useful if every TextBox needs to execute the same code.

    To add an arbitrary event like an Enter keypress, you might be better off looking into Attached Events. I believe this can get you what you want.

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

Sidebar

Related Questions

Hello I am trying to handle the Application.LoadCompleted event but it's never triggered. Instead
...instead of using the Atom syndication format? Atom is a well-defined , general-purpose XML
Instead of writing my ASP.NET C# applications in Visual Studio, I used my favorite
Instead of relying on my host to send an email, I was thinking of
Instead of returning a common string, is there a way to return classic objects?
Instead of having to remember to initialize a simple 'C' structure, I might derive
Instead of hardcoding the default @author template I would like Eclipse to use user's
Instead of writing ((x: Double) => (((y: Double) => y*y))(x+x))(3) I would like to
Instead of just filling out the form in HTML it should also be possible
instead of doing session(myvar1) = something session(myvar2) = something session(myvar3) = something session(myvar4) =

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.