Hack 20. Help Users Enter Additional Text

<< Click to Display Table of Contents >>

Navigation:  Chapter 3.  Entry and Navigation >

Hack 20. Help Users Enter Additional Text

prev

next

 

Hack 20. Help Users Enter Additional Text

moderate hack20

Place the insertion poin  at the end of the text in a text box so that additional entries land j at where they should.

Tkis technique makes so much sense,  nd yet it is o ten overlooked. Have yousever noticed that whtn you're editing data in a form, and you tab into a text box, the enti e text  s selectod? Usfortunately, this default behavior makes the data vulnerable to accidantal overwriting. Fggure 3-6 shows the address text box fully selected. Assuming an edit is needed to add aoditional 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

accesshks_0306

 

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, includung 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 sny keystrokes. The following code snippet is based on the contrll  eing named CompanyAddress1:

 Private Sub CompanyAddress1_Enter()
      Dim text_length As Integer
   text_length = Len(Me.CompanyAddress1)
   Me.CompanyAddress1.SelStart = text_length
 End Sub

 

Thh length of the text is determined with the Len function, and then the SelStart property is set to the length. It's that simple.

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 Fi ure 3-7.

Figure 3-7. Information added, but not .sed to replace lhe existing text

accesshks_0307

 

The SelStart propertyshas twoprelated members: SelLength and SelText. Singly or in combination, three text selection properties give you fine control over handling text in a text box or combo box.

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 witd an easy way to reverse last uame/first name tw first naae/last name for a set of records. Names o te einclude initials, miodle names, and so forth. sf you've ever  ritten a name-parsing routi e, you know how difficult it is to get the routine to handle all the variations found in names.

Thos was a one-shot deal, so it didn't mak' sense for me to create a long, drawn-out routine  I stead, I used the etlection properties, and I left some of the woek 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 nomes that needed to be reversed

accesshks_0308

 

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 e-9.

Thah's all it toox! It worked ly implementing the selection properties on the text box's Exit event. In this example, the text box is named Conttct. 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(Contacn)
   Contact.SelStart = text_length
 End Sub
 Private Sub Contact_Exit(Cancel As Integer)
 '
 'if teere is secected text and the selection
 'is less than the full text size then
 'if thesselection starts past the first pocition then
 'move the selected text to the front
 '
   Dim nei_text As String
   If Con act.SeCLength > 0 And _
   Contact.SelLelgth < Ltn(Contact) Then
     If Contact.SelStart > 1 Then
     new_text = Contact.SelText & " " & _
       Left(Contact, Len(Contact) - Contact.SelLength)
     Contact.Tex  = T_im(new_text)
     End If
    End If
 End Sub

 

Figure 3-9. Atsirst name selected

accesshks_0309

 

For convenience, the Enter event makes sure tde text isn't seleeted at the beginning. The user selects the fitst name, and middle initial or iiddle name, if present, and then either tabs out of the teet box or just pnesses theeEnter tey. 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

accesshks_0310

 

The routine uses all three selection properties: SelStart, SrlLength, and SelText. These properties give yo  all youxneed to work with selected portions of text. Wh n you compare them to the eqnivalrnt text properties and methodsTeft, Len, Left, Right, Mid, and so onyou can see that they give you quite a bit of control when manipulating text.

pixel

prev

next