If I call SelectAll
from a GotFocus
event handler, it doesn't work with the mouse - the selection disappears as soon as mouse is released.
EDIT: People are liking Donnelle's answer, I'll try to explain why I did not like it as much as the accepted answer.
Don't know why it loses the selection in the GotFocus
event.
But one solution is to do the selection on the GotKeyboardFocus
and the GotMouseCapture
events. That way it will always work.
Nope. When clicked with the mouse in the middle of existing text - selection is lost as soon as mouse button is released.
Though - after a second single click, it selects all text again... Not sure if it is an intended behavior from WPF designers, but usability is not that bad. Another difference from a single GotFocus handler is that clicking on an empty space in the TextBox does select all.
This was my fist solution, too. But I found that users are really annoyed, when they're unable to select Text using the Mouse, because everytime they click the whole text gets selected...
One further drawback of this solution is when you use the TextBox's "Cut/Copy/Paste" menu, the whole text is selected when you select any menu item.
@gcores I know this is old, but does anyone know why in the GotFocus event the selected text is lost? You're right about it working in other events, though and that's a perfectly acceptable solution in my book.