When a C# WinForms textbox receives focus, I want it to behave like your browser's address bar.
To see what I mean, click in your web browser's address bar. You'll notice the following behavior:
- Clicking in the textbox should select all the text if the textbox wasn't previously focused.
- Mouse down and drag in the textbox should select only the text I've highlighted with the mouse.
- If the textbox is already focused, clicking does not select all text.
- Focusing the textbox programmatically or via keyboard tabbing should select all text.
I want to do exactly this in WinForms.
FASTEST GUN ALERT: please read the following before answering! Thanks guys. :-)
Calling .SelectAll() during the .Enter or .GotFocus events won't work because if the user clicked the textbox, the caret will be placed where he clicked, thus deselecting all text.
Calling .SelectAll() during the .Click event won't work because the user won't be able to select any text with the mouse; the .SelectAll() call will keep overwriting the user's text selection.
Calling BeginInvoke((Action)textbox.SelectAll) on focus/enter event enter doesn't work because it breaks rule #2 above, it will keep overriding the user's selection on focus.