tags:

views:

654

answers:

3

How do you auto highlight text in a textbox control when the control gains focus.

+3  A: 

In Windows Forms and WPF:

textbox.SelectionStart = 0;
textbox.SelectionLength = textbox.Text.Length;
Reed Copsey
+1  A: 

In asp.net:

textBox.Attributes.Add("onfocus","this.select();");
beon
A: 

If you need to do this for a large number of textboxes (in Silverlight or WPF), then you can use the technique used in the blog post: http://dnchannel.blogspot.com/2010/01/silverlight-3-auto-select-text-in.html. It uses Attached Properties and Routed Events.