Click here to Skip to main content
Click here to Skip to main content
Technical Blog

How can you add "Select All" behaviour for all TextBoxes in your WPF application?

, 24 Aug 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Add "Select All" behaviour for all TextBoxes in your WPF application

While working in Windows, you have seen that while focusing on any TextBox, the entire text inside it has been selected. This is a default behaviour of Windows, but in WPF, this behaviour is not present. For this, you have to write code.

If you want to add the same behaviour in your WPF application, then you have to register the TextBox.GotFocus event for each textbox inside your Window and in that event implementation you have to write the code to select all the text inside the focused TextBox, right? If you do like this, then you have to write so many event registrations for each one of the TextBoxes.

Let's say, you have a single Window with 100 TextBoxes in it. In this case though, you can write the event definition inside a single implementation but you have to register the event for 100 times. Now imagine the same behaviour for 10 Windows inside the same application. Here you have to write the event implementation for each and every Window & register the event for each TextBox. This will definitely clutter your code.

So, how can you get the said behaviour in your WPF application? It is a simple trick inside your App.xaml.cs file. This will be a global event handler for any TextBox in your application. If you open your App.xaml.cs, you will find an overridable method named "OnStartUp". Inside this, register the TextBox.GotFocus event for every TextBox before calling the OnStartup method for the base class. After doing this trick, your OnStartup method will look like this:

protected override void OnStartup(StartupEventArgs e)
{
     EventManager.RegisterClassHandler(typeof(TextBox), 
                                       TextBox.GotFocusEvent, 
                                       new RoutedEventHandler(TextBox_GotFocus)
                                      );
     base.OnStartup(e);
}

Here, the EventManager class will be responsible to register the event handler for all TextBox. So simple right? Now add the event handler implementation for the TextBox in the same class. This will look like this:

private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
     TextBox tb = sender as TextBox;
     tb.Focus();
     tb.SelectAll();
}

Now run your application with multiple Windows having multiple TextBoxes on them. Focus on any TextBox having some text inside it. You will see the same behaviour has been added to all of your TextBoxes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kunal Chowdhury (@kunal2383)
Team Leader
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.
 
Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralSilverlight? PinmemberMember 873203116-Aug-12 8:06 
GeneralMy vote of 2 Pinmemberhaindl24-Aug-09 22:27 
GeneralRe: My vote of 2 PinmemberKunalChowdhury30-Aug-09 22:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 24 Aug 2009
Article Copyright 2009 by Kunal Chowdhury (@kunal2383)
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid