Created
February 22, 2012 19:30
-
-
Save kieranajp/1886791 to your computer and use it in GitHub Desktop.
Creating a System.Windows.Forms.TextBox that automatically selects text on focus (like a browser's URL bar)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FocusTextBox : System.Windows.Forms.TextBox | |
{ | |
private bool _focussed; | |
protected override void OnEnter(EventArgs e) | |
{ | |
base.OnEnter(e); | |
if (MouseButtons == MouseButtons.None) | |
{ | |
SelectAll(); | |
_focussed = true; | |
} | |
} | |
protected override void OnLeave(EventArgs e) | |
{ | |
base.OnLeave(e); | |
_focussed = false; | |
} | |
protected override void OnMouseUp(MouseEventArgs mevent) | |
{ | |
base.OnMouseUp(mevent); | |
if (!_focussed) | |
{ | |
if (SelectionLength == 0) | |
SelectAll(); | |
_focussed = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment