[RESOLVED] [2.0] Selecting textbox text upon gaining focus
Hi all,
I am trying to select all text in a textbox when it gains focus, either by keyboard or mouse - as is common in UI's. I have tried using the GotFocus and Enter events but neither of them worked when clicking in the textbox. Seems they fire too early and the selection is overriden by the mouse click.
Here is the code I used
Code:
void textbox_Enter(object sender, EventArgs e)
{
((TextBox)sender).SelectAll();
}
As I said above it works with keyboard but not with mouse. So can anyone advise me of a decent method to achieve this effect both ways?
Thanks
- P
Re: [2.0] Selecting textbox text upon gaining focus
Place the code in the MouseUp event too.
So what your after is a textbox that continuously selected no matter the action?
Re: [2.0] Selecting textbox text upon gaining focus
I found this description of events that should help.
Quote:
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:
Enter
GotFocus
LostFocus
Leave
Validating
Validated
If the CausesValidation property is set to false, the Validating and Validated events are suppressed.
Re: [2.0] Selecting textbox text upon gaining focus
It fires - it's just that if I use the mouse, the selection doesn't happen.
I don't want it permanently selected. Just when it gains focus.
Edit: Works in MouseUp. I would simply rather not have to trap two events, but oh well.
Thanks.
Re: [2.0] Selecting textbox text upon gaining focus
Just tested on an open VB.net project and this works for the mouse.
VB Code:
Private Sub txtAddress1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtAddress1.MouseUp
Me.txtAddress1.SelectAll()
End Sub
Edit: Just 1 minute too late. :D
Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus
This is actually more complex than you think. If you handle the MouseUp, Click or MouseClick event and call SelectAll there, what happens if the control already has focus and the user clicks it to place the caret? Bam! SelectAll.
Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus
Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus
So then what, use a boolean flag in the Enter event to signify that its the first time selecting the text. Then if they are using the mouse to re-focus you bypass selecting the text. Probably something along those lines.
Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus
Indeed. Use the same event handlers for all the TextBoxes you want to behave this way:
Code:
private bool selectAll = true;
private void TextBox_Enter(object sender, EventArgs e)
{
((TextBox)sender).SelectAll();
}
private void TextBox_MouseClick(object sender, MouseEventArgs e)
{
if (this.selectAll)
{
((TextBox)sender).SelectAll();
this.selectAll = false;
}
}
private void TextBox_Leave(object sender, EventArgs e)
{
this.selectAll = true;
}
Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus
Actually that's imperfect too. If you tab in and then move the caret with the keyboard then click you will reselect everything. You'd probably have to add a KeyPress event handler as well and set selectAll to False.
Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus
KeyPress isn't raised by the arrow keys so I think adding this should sort you out:
Code:
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
this.selectAll = false;
}
Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus
Thanks. I'll try that all out tommorow. :)
Re: [RESOLVED] [2.0] Selecting textbox text upon gaining focus
Quote:
Originally Posted by penagate
Thanks. I'll try that all out tommorow. :)
Now, d*mn it. Now. I'm still working in Sydney and it's earlier in Adelaide. Slacker. ;)