Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 909
Jiri Avatar asked Dec 28 '22 19:12

Jiri


2 Answers

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

like image 58
adrianbanks Avatar answered Jan 10 '23 00:01

adrianbanks


You can also use textBox.DeSelectAll().

like image 40
Hemal Avatar answered Jan 10 '23 00:01

Hemal