Jim McCurdy's Tech Blog

Insights into Software Development and Silverlight

A TextBox that selects its text on focus for Silverlight

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

    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!!!

    • jim mccurdy

      5/31/2010 9:04:26 PM |

      Your welcome Nishanth...

  • Will

    6/15/2010 2:45:53 PM |

    Very nice. thanks!

  • Althaf Moideen

    2/3/2011 4:59:42 AM |


    Very nice. thanks!

  • tercüme

    4/25/2011 6:34:04 AM |

    very good share. thank you so much.

  • 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.

  • timelesswrought

    4/19/2012 7:25:16 AM |

    Thanks Jim. Keep on suggesting useful topics

  • 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();
          }
        }

Comments are closed