views:

133

answers:

1

Hello,

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

+1  A: 

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

adrianbanks
Oh, that works, thanks! I guess there is a difference when just setting the selection start and selection length, and using .Select. Also, textbox.Select(textbox.selectionstart, textbox.selectionstart) works better for my needs.
Jiri