Jiri
Jiri

Reputation: 264

Prevent automatic selection on textbox focus

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 (-:

Upvotes: 4

Views: 6880

Answers (2)

Hemal
Hemal

Reputation: 3760

You can also use textBox.DeSelectAll().

Upvotes: 1

adrianbanks
adrianbanks

Reputation: 82934

(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.

Upvotes: 9

Related Questions