I put on a winform a textbox and then in Enter event i execute the following code to select the text when the text box get the focus:
private void textBox1_Enter(object sender, System.EventArgs e)
{
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Length;
}
This code doesn't work. Where is the problem

Select textbox text
Silver
TextBox1.SelectAll()
Now no matter how the TextBox receives focus (mouse click, tab key, Focus() method) the entire text will be selected. Keep in mind however that if you modify the text selection on any of the mouse button events, the user will not be able to select partial text with the mouse; they will have to use the keyboard.
ali rizwan
If you put only a text box on a winform and then start the application, the text is selected by default.
welcomeguy
Aaron128
this is my working solution
Public Class MyTextBox
Inherits TextBox
private selecting as boolean
Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
MyBase.OnEnter(e)
selecting = True
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
If selecting Then
selecting = False
Me.SelectAll()
End If
End Sub
End Class
Runaway
The reason for this is that when selected with the mouse, the TextBox.Enter event is fired *before* the TextBox.MouseDown event. The MouseDown event then sets the SelectionStart property to whever the mouse was clicked. So, if you were to investigate further, you'd see that your code is, in fact, running, but the SelectionStart is overridden by the MouseDown.
The solution is to simply move your code from the Enter event to the MouseDown event.
Elyahoo
Todd Girvin
I am overiding the onmousedown sub and after I run the base.onmousedown I set selectionstart = 0 and selectionlength = 0. So, when I exit the Sub selectionlength is zero. But if I set a breakpoint on MouseUp, the selection length is > 0. So what code is running in between mousedown and mouseup that is making the selection