Automatic Selection Of Text In VB4

I have developed a global subroutine that will highlight the text of the active control of the active form.

The routine is simple:

Sub Highlight()
    On Error Resume Next
    Dim FRM1 As Form
    Set FRM1 = Screen.ActiveForm
    FRM1.ActiveControl.SelStart = 0
    FRM1.ActiveControl.SelLength = Len(FRM1.ActiveControl)
End Sub

Create a form object by first dimensioning it and setting it to the active form. Next, select the text of the form’s active control starting at the left most character to the end of the text.

Use this in the got_focus method of any control that allows text entry such as a text box or a combo box.

Private Sub Text1_GotFocus ()
    Call Highlight
End Sub

That’s it! One subroutine call does it all. I put this routine in a global module so all my forms have access to it.


Tip Submitted By: Marc Mueller

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s