A TextBox that selects its text on focus for Silverlight

by Jim McCurdy 28. August 2009 22:36

A minor annoyance of mine is that there is no way to wire up a standard Silverlight TextBox to select its text when it receives the keyboard focus; either via a mouse click or a tab key.

And since users are accustomed to web apps, browsers, and desktop applications that offer the the convenience of selecting textbox text  upon focus, I wanted that behavior in my Silverlight applications.   So to satisfy user expectations as a matter of consistency, I wrote a very simple derived class, TextBoxEx, that will offer this functionality.  The TextBoxEx class derives from TextBox, and can be referenced in XAML for any and all of your TextBox’s.  There are no methods to call.  It just listens for Focus events and selects it own text.  Very simple.

Usage is as follows:

  • In XAML, reference the assembly where you implement the TextBoxEx class listed below, and add as many TextBoxEx elements as you need.  The example below uses data binding to display a username.
<UserControl x:Class="MyApp.MainPage"
	xmlns="http://schemas.microsoft.com/client/2007"     
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
	xmlns:c="clr-namespace:ClassLibrary;assembly=ClassLibrary"  
>  
.     
.     
.     
<c:TextBoxEx Text="{Binding User.Name, Mode=TwoWay}" Width="120" />

The code below has been tested and used with Silverlight 3 and Silverlight 4.

using System.Windows;
using System.Windows.Controls;

namespace ClassLibrary
{
	// This TextBox derived class selects all text when it receives focus
	public class TextBoxEx : TextBox
	{
		public TextBoxEx()
		{
			base.GotFocus += OnGotFocus;
		}

		private void OnGotFocus(object sender, RoutedEventArgs e)
		{
			base.SelectAll();
		}
	}
}

Comments (8) -

Nishanth Nair
Nishanth Nair
5/28/2010 10:24:54 PM #

Thanks a lot Jim! I badly wanted this feature for a resource planning datagrid and you made my day!!!

Reply

jim mccurdy
jim mccurdy
5/31/2010 9:04:26 PM #

Your welcome Nishanth...

Reply

Will
Will
6/15/2010 2:45:53 PM #

Very nice. thanks!

Reply

Althaf Moideen
Althaf Moideen
2/3/2011 4:59:42 AM #


Very nice. thanks!

Reply

terc&#252;me
tercüme
4/25/2011 6:34:04 AM #

very good share. thank you so much.

Reply

M.Sathakkathul Haq
M.Sathakkathul Haq
8/5/2011 5:29:52 AM #

Hi Jim,

Your article is perfectly applicable for my requirements.

It's really saved my time.

Im looking for more articles.

Thanks a lot.

Reply

timelesswrought
timelesswrought
4/19/2012 7:25:16 AM #

Thanks Jim. Keep on suggesting useful topics

Reply

moh
moh
4/25/2012 6:07:16 AM #

<TextBox  Name="tbName1"  GotFocus="tb_GotFocus" />
<TextBox  Name="tbName2"  GotFocus="tb_GotFocus" />

    private void tb_GotFocus(object sender, System.Windows.RoutedEventArgs e)
    {
      TextBox tb = sender as TextBox;
      if (tb != null && !string.IsNullOrEmpty(tb.Text))
      {
        tb.SelectAll();
      }
    }

Reply

Pingbacks and trackbacks (2)+

Add comment

biuquote
  • Comment
  • Preview
Loading

About Me

My Photo Jim is always looking for new clients in need of software development expertise.  He is particularly well suited for Silverlight, WPF, and ASP.NETprojects.

Professional Biography

Jim McCurdy operates Face to Face Software, where he works designing, developing, and managing software projects for a variety of clients.

Jim currently specializes in contract development projects for Silverlight, WPF, and ASP.NET platforms. Recent projects include two complete web sites developed using Silverlight, .NET, and C#. The first is YinYangMoney.com, a unique financial and lifestyle planning web application. The second is McPivot.com, a unique web application that presents custom queries and visualization of baseball data back to the 1800's.

Jim has worked in the software industry for as long as he can remember, and has broad expertise in Web, Windows, and systems technologies. Jim has been a team player on many agile development projects throughout his career, and is a founding member at software startups Astral Development and Powerhouse Entertainment.

You can Email Jim at Face to Face Software.

Blog Archive

Welcome Readers