Avatar of P1ST0LPETE
P1ST0LPETE
Flag for United States of America

asked on 

Highlight/SelectAll() text in a textbox when textbox has focus - C#

Hi all,
I'm creating a WPF windows application using C# and the .net framework 3.5.  One of my windows is a huge form with about 60 textboxes on it.  To make things easier on the user (and up to the standards of todays forms), I need to have the text of each textbox be highlighted when a user tabs into it.  Attached is my current code, which works, but there has got to be a better, more correct, more dynamic way of doing this with less lines of code.

I'm using one method event handler, and assigning the "GotFocus()" event of each textbox to this method.  I think if I had a way of determining the name of the textbox that fired the TextBoxHighlightOnTabStop() event handler, it would be faster then checking each and every textbox if it has focus.  Any ideas?

Thamks,
Pete
private void TextBoxHighlightOnTabStop(object sender, RoutedEventArgs e)
{
        if (tbControlName.IsFocused) tbControlName.SelectAll();
        else if (tbTime.IsFocused) tbTime.SelectAll();
        else if (tbInfo.IsFocused) tbInfo.SelectAll();
        //Rest of the 60 textboxes......
}

Open in new window

Microsoft Development.NET ProgrammingC#

Avatar of undefined
Last Comment
P1ST0LPETE

8/22/2022 - Mon