Hack 20. Help Users Enter Additional Text
This technique makes so much sense, and yet it is often overlooked. Have you ever noticed that when you're editing data in a form, and you tab into a text box, the entire text is selected? Unfortunately, this default behavior makes the data vulnerable to accidental overwriting. Figure 3-6 shows the address text box fully selected. Assuming an edit is needed to add additional text (not to replace the text), the user must move his mouse to the end of the text and then click to deselect it.
Figure 3-6. Automatically selected data, vulnerable to an accidental delete or overwrite
Wouldn't it be nice if the user didn't have to click first to deselect the text? Of course, there is a way to do this. It takes just a smattering of code.
Many controls, including text boxes, have an Enter event, which is triggered when the control is clicked or tabbed into. This is the event in which you can place code to move the cursor to the end of the text, before the user has a chance to enter any keystrokes. The following code snippet is based on the control being named CompanyAddress1:
Private Sub CompanyAddress1_Enter() Dim text_length As Integer text_length = Len(Me.CompanyAddress1) Me.CompanyAddress1.SelStart = text_length End Sub
You can add a routine such as this to all the text boxes in a form, if it makes sense to do so. Is data usually overwritten, or does it receive additional characters? Only you know your application, so only you can decide where to incorporate this code. In this example, some additional information has been added to the address, as shown in Figure 3-7.
Figure 3-7. Information added, but not used to replace the existing text
So far, this hack has shown you how to use SelStart to set where the entry will begin. But once I had to provide a user with an easy way to reverse last name/first name to first name/last name for a set of records. Names often include initials, middle names, and so forth. If you've ever written a name-parsing routine, you know how difficult it is to get the routine to handle all the variations found in names.
This was a one-shot deal, so it didn't make sense for me to create a long, drawn-out routine. Instead, I used the selection properties, and I left some of the work up to the user. Here's how it worked.
I presented the contact names that had to be reversed in a form, such as that shown in Figure 3-8.
Figure 3-8. The names that needed to be reversed
The user simply selected the first nameor first name and initial, or first name and middle name, and so onand then pressed either the Tabor the Enter key, as shown in Figure 3-9.
That's all it took! It worked by implementing the selection properties on the text box's Exit event. In this example, the text box is named Contact. The routine explained earlier, which uses the Enter event, is also used in this example:
Private Sub Contact_Enter() ' 'remove selection effect 'and place insertion point at end ' Dim text_length As Integer text_length = Len(Contact) Contact.SelStart = text_length End Sub Private Sub Contact_Exit(Cancel As Integer) ' 'if there is selected text and the selection 'is less than the full text size then 'if the selection starts past the first position then 'move the selected text to the front ' Dim new_text As String If Contact.SelLength > 0 And _ Contact.SelLength < Len(Contact) Then If Contact.SelStart > 1 Then new_text = Contact.SelText & " " & _ Left(Contact, Len(Contact) - Contact.SelLength) Contact.Text = Trim(new_text) End If End If End Sub
Figure 3-9. A first name selected
For convenience, the Enter event makes sure the text isn't selected at the beginning. The user selects the first name, and middle initial or middle name, if present, and then either tabs out of the text box or just presses the Enter key. This fires the Exit event, which tests the text to make sure something has been selected, it isn't the same length as the entire text, and it doesn't start at the beginning.
The routine then reverses the selected and unselected portions (the first name and the last name) and returns them to the text box. Figure 3-10 shows the result.
Figure 3-10. All the names reversed
The routine uses all three selection properties: SelStart, SelLength, and SelText. These properties give you all you need to work with selected portions of text. When you compare them to the equivalent text properties and methodsText, Len, Left, Right, Mid, and so onyou can see that they give you quite a bit of control when manipulating text.