First of all, thanks for answers! 9 total answers. Thank you.
Bad news: all of the answers had some quirks or didn't work quite right (or at all). I've added a comment to each of your posts.
Good news: I've found a way to make it work. This solution is pretty straightforward and seems to work in all the scenarios (mousing down, selecting text, tabbing focus, etc.)
bool alreadyFocused;
...
textBox1.GotFocus += textBox1_GotFocus;
textBox1.MouseUp += textBox1_MouseUp;
textBox1.Leave += textBox1_Leave;
...
void textBox1_Leave(object sender, EventArgs e)
{
alreadyFocused = false;
}
void textBox1_GotFocus(object sender, EventArgs e)
{
// Select all text only if the mouse isn't down.
// This makes tabbing to the textbox give focus.
if (MouseButtons == MouseButtons.None)
{
this.textBox1.SelectAll();
alreadyFocused = true;
}
}
void textBox1_MouseUp(object sender, MouseEventArgs e)
{
// Web browsers like Google Chrome select the text on mouse up.
// They only do it if the textbox isn't already focused,
// and if the user hasn't selected all text.
if (!alreadyFocused && this.textBox1.SelectionLength == 0)
{
alreadyFocused = true;
this.textBox1.SelectAll();
}
}
As far as I can tell, this causes a textbox to behave exactly like a web browser's address bar.
Hopefully this helps the next guy who tries to solve this deceptively simple problem.
Thanks again, guys, for all your answers that helped lead me towards the correct path.
I found a simpler solution to this. It involves kicking off the SelectAll asynchronously using Control.BeginInvoke
so that it occurs after the Enter and Click events have occurred:
In C#:
private void MyTextBox_Enter(object sender, EventArgs e)
{
// Kick off SelectAll asynchronously so that it occurs after Click
BeginInvoke((Action)delegate
{
MyTextBox.SelectAll();
});
}
In VB.NET (thanks to Krishanu Dey)
Private Sub MyTextBox_Enter(sender As Object, e As EventArgs) Handles MyTextBox.Enter
BeginInvoke(DirectCast(Sub() MyTextBox.SelectAll(), Action))
End Sub
Your solution is good, but fails in one specific case. If you give the TextBox focus by selecting a range of text instead of just clicking, the alreadyFocussed flag doesn't get set to true, so when you click in the TextBox a second time, all the text gets selected.
Here is my version of the solution. I've also put the code into a class which inherits TextBox, so the logic is nicely hidden away.
public class MyTextBox : System.Windows.Forms.TextBox
{
private bool _focused;
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
if (MouseButtons == MouseButtons.None)
{
SelectAll();
_focused = true;
}
}
protected override void OnLeave(EventArgs e)
{
base.OnLeave(e);
_focused = false;
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
base.OnMouseUp(mevent);
if (!_focused)
{
if (SelectionLength == 0)
SelectAll();
_focused = true;
}
}
}
It's a bit kludgey, but in your click event, use SendKeys.Send( "{HOME}+{END}" );
.
Click event of textbox? Or even MouseCaptureChanged event works for me. - OK. doesn't work.
So you have to do 2 things:
private bool f = false;
private void textBox_MouseClick(object sender, MouseEventArgs e)
{
if (this.f) { this.textBox.SelectAll(); }
this.f = false;
}
private void textBox_Enter(object sender, EventArgs e)
{
this.f = true;
this.textBox.SelectAll();
}
private void textBox_MouseMove(object sender, MouseEventArgs e) // idea from the other answer
{
this.f = false;
}
Works for tabbing (through textBoxes to the one) as well - call SelectAll() in Enter just in case...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With