Select textbox text

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


Answer this question

Select textbox text

  • Silver

    In the TextBox GotFocus and Click events, put the code:

    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

    The entire text of a Textbox is NOT selected on enter by default: put a button on a winform and then a text box, start the application and then set the focus on the text box. The text is not selected by default. It's for this reason that i wrote code in the Enter event.

    If you put only a text box on a winform and then start the application, the text is selected by default.

  • welcomeguy

    How are you setting focus on the TextBox   If you tab through your controls, you'll notice that the entire text is selected each time the TextBox receives focus.  If you set it programmatically, the same thing happens.
  • Aaron128

    The problem occurs because when you give it focus by clicking, not only does the text box raise the Enter event, but also the mouse events as well. So, even though you are telling the textbox to select on enter (which appears to be correct), the control receives a mouse click event and repositions the cursor at the point you clicked, thus clearing your selection.

    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 entire text of a Textbox is selected on enter by default, so I'm assuming that what you mean when you say this doesn't work is that it doesn't work when the Textbox is selected with the mouse.  

    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

    if your code is to hightlighted the text in textbox1, remember to set HideSelection to be false in the textbox's property.
  • Todd Girvin

    I'm trying to always have the cursor start at the beginning (positon 0) when a user clicks into the textbox with the mouse.

    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

  • Select textbox text