.net – Prevent automatic selection on textbox focus

controlsnettextboxvb.netwinforms

When you use a tab key to select a textbox, all text in it is automatically selected. What's the easiest way to prevent this from happening? (Setting the selection to none in Enter or GotFocus events doesn't work)

Thanks (-:

Best Answer

(I'm assuming that you are using WinForms)

What you have said you have already tried does work.

If you handle the Enter event on the text box, you can set the selection to nothing:

Private Sub textBox_Enter(ByVal sender As Object, ByVal e As EventArgs)
    Dim position As Integer = textBox.Text.Length
    textBox.Select(position, position)
End Sub

This sets the selection to be a zero-length string starting at the end of the text currently in the text box. This is to position the caret at the end of the current text.