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

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

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

"_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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.