0

When I enter a vb.net textbox, if there is data in it, I want it to be highlighted. I looked around and couldn't find a property that would do this so I do the following:

I built a little subroutine that does the highlighting and then call it from the ENTER and CLICK event on each text box.

Private Sub fixFocus(sender As Object)
        sender.SelectionStart = 0
        sender.SelectionLength = Len(sender.Text)

    End Sub

    Private Sub txtCompAddress_Click(sender As Object, e As System.EventArgs) Handles txtCompAddress.Click
        fixFocus(sender)
    End Sub

    Private Sub txtCompAddress_Enter(sender As Object, e As System.EventArgs) Handles txtCompAddress.Enter
        fixFocus(sender)
    End Sub
3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by codeorder
0

Set both properties SelectionStart to zero and SelectionLength to the lenght of the text inside:

textBox1.SelectionStart = 0
textBox2.SelectionLength = textBox1.Text.Lenght
0

"_GotFocus" and other events are in the "_GotFocus".

Private Sub _TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus '// TextBoxEtc.GotFocus
        With CType(sender, TextBox)
            If .Text.Length > 0 Then .SelectAll()
        End With
    End Sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.