General discussion

Locked

Select all text when Text Box receives the focus

By MaryWeilage Editor ·
This week's Visual Basic e-newsletter explains how to select all text when a Text Box gets the focus.

Is this tip useful to you? If not, please tell us what topics you'd like to see us cover in this e-newsletter.

If you aren't subscribed to the free Visual Basic e-newsletter, click the link below to automatically sign up:
http://nl.com.com/MiniFormHandler?brand=builder&list_id=e065

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Another Method

by randy In reply to Select all text when Text ...

I use the following code to do the same but without passing the control name each time.

Private Sub Text23_GotFocus()
SelAllText
End Sub

Public Sub SelAllText()
If Not Screen.ActiveControl Is Nothing Then
Screen.ActiveControl.SelStart = 0
Screen.ActiveControl.SelLength = Len(Screen.ActiveControl.text)
End If
End Sub

Collapse -

Undo aswell

by dracowiz In reply to Another Method

I have been using the desribed method for awhile, but my users have complained about inadvertently deleting desired text without recourse. So I have amended the function to copy the text to the clipboard (or alternately another dummy text field) so that the user can retrieve the old text with the control z function similiar to word's udo function though only good for one level


Private Sub txtFields_GotFocus(Index As Integer)
If defaultTextSelect Then
txtFields(Index).Text = Trim(txtFields(Index).Text)
txtFields(Index).SelStart = 0
txtFields(Index).SelLength = Len(txtFields(Index).Text)
End If
Clipboard.SetText txtFields(Index).Text
End Sub

Private Sub txtFields_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
Dim tmpText As String
Dim ShiftKey As Long
ShiftKey = Shift And 7
Select Case ShiftKey
Case 2 ' or vbCtrlMask
Select Case KeyCode
Case vbKeyZ
tmpText = txtFields(Index).Text
txtFields(Index).Text = Clipboard.GetText
Clipboard.SetText tmpText
End Select
End Select
End Sub


This alternates the text box with data prior to Ctrl-Z and data at got focus -- keeps my users happy.

DracoWiz

Back to Web Development Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums