Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software

I think that any business application should select all the text in a TextBox when the user tabs into it.  Your advanced users that take advantage of the tab key will appreciate it.  In WinForms 2.0, the best way of accomplishing this was to create a new control that inherited from TextBox that implemented SelectAll functionality and use your new control instead of the normal TextBox.

In WPF, this is much easier.  In the App.xaml.cs, override the OnStartup method and use the EventManager.RegisterClassHandler to listen to the GotFocusEvent of every TextBox:

 

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

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

If you want to override this behavior for a specific TextBox, just point the GotFocus event to a method like this:

void NoSelectTextBox_GotFocus(object sender, RoutedEventArgs e)
{
   (sender as TextBox).Select(0, 0);
}
 
Posted on Tuesday, September 18, 2007 8:29 AM .NET , WPF | Back to top


Comments on this post: WPF - TextBox.SelectAll

# re: WPF - TextBox.SelectAll
Requesting Gravatar...
You rock! Now make it work in Silverlight 1.1....

:P
Left by Doug Butscher on Nov 05, 2007 10:30 PM

# re: WPF - TextBox.SelectAll
Requesting Gravatar...
You shouldn't inherit from the textbox to add this functionality. You should instead use attached properties and behaviors to accomplish this. You would still be able to provide the same functionality without being forced to use a single textbox control.
Left by Morder on Sep 16, 2008 3:05 AM

# re: WPF - TextBox.SelectAll
Requesting Gravatar...
love it! now my WPF looks so biznessy
Left by jp on Mar 27, 2009 12:36 PM

# re: WPF - TextBox.SelectAll
Requesting Gravatar...
This technique is not only interesting for textboxes, it shows a "paradigm" in setting common behavior or GUI elements of the same class, i.e. a programmatic style. Really fantastic blog post of yours!

Thanks,
Tore, Norway
Left by Tore Aurstad on Jul 28, 2009 12:20 PM

# re: WPF - TextBox.SelectAll
Requesting Gravatar...
Thanks, very very helpful. To make it work also on mouse click in the textbox capture the GotMouseCapture event as well

EventManager.RegisterClassHandler(typeof(TextBox),
TextBox.GotMouseCaptureEvent,
new RoutedEventHandler(TextBox_GotMouseCapture));


void TextBox_GotMouseCapture(object sender, RoutedEventArgs e)
{
//(sender as TextBox).Focus();
(sender as TextBox).SelectAll();
}
Left by Tim Burnett on Oct 07, 2009 5:36 PM

# re: WPF - TextBox.SelectAll
Requesting Gravatar...
Interesting post and I really like your take on the issue. I now have a clear idea on what this matter is all about. Thank you so much.
Left by Druckvorstufe on May 28, 2010 4:41 AM

# re: WPF - TextBox.SelectAll
Requesting Gravatar...
This very helpful.
Thanks
Left by boymletes on Aug 26, 2011 3:42 AM

# re: WPF - TextBox.SelectAll
Requesting Gravatar...
EventManager.RegisterClassHandler(typeof(TextBox),
TextBox.GotMouseCaptureEvent,
new RoutedEventHandler(TextBox_GotMouseCapture));


void TextBox_GotMouseCapture(object sender, RoutedEventArgs e)
{
//(sender as TextBox).Focus();
(sender as TextBox).SelectAll();
}

This one very helpfull,Resolved my issue,Thank you very much.
Left by Bob on Jun 26, 2012 3:48 AM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net