Skip to content

Instantly share code, notes, and snippets.

@kieranajp
Created February 22, 2012 19:30
Show Gist options
  • Save kieranajp/1886791 to your computer and use it in GitHub Desktop.
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)
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