How to Capitalize Some Characters in a Text Box Automatically

Last reviewed: April 1, 1996
Article ID: Q148918
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, versions 3.0, 3.0b

SUMMARY

When typing names into a text box, you can have the first letter of each word automatically capitalized. This can be done in the KeyPress event by checking each character that is typed and capitalizing it if it is the first character entered or if the character before it is blank. Names such as "Van Camp" would have the first letter of each part of the name automatically capitalized. However, names like "MacDonald" would still require that you manually capitalize the D key.

MORE INFORMATION

Step-by-Step Procedure

The following steps illustrate how to use the KeyPress event to check each character and enforce the rule that the first character of each word be capitalized.

  1. Create a new form, and place a text box on it.

  2. Create a new property for the form. On the Form menu, click New Property, and in the Name box, type Lastchar. Click OK, and return to the form.

  3. In the When Event of the text box, place the following code:

          THISFORM.lastchar = 32
    

    This assigns the numeric value of a space (32) to the lastchar variable.

  4. In the KeyPress event of the text box, place the following code:

       IF THISFORM.lastchar = 32   && Checks lastchar to see if it is
                                   && equal the numeric value of a space.
           NODEFAULT               && Doesn't allow the character to be
                                   && entered because it is lowercase.
           KEYBOARD UPPER(CHR(nKeyCode))  && Places the character into the
                                          && text box in uppercase.
       ENDIF
       IF nKeyCode=127 AND LEN(ALLTRIM(this.value))=1  &&  Checks to see if
                                       && the user has backspaced over
                                       && the first letter in the text box.
           THISFORM.lastchar=32        && Assigns numeric value of a space
                                       && to lastchar.
       ELSE
           THISFORM.lastchar= nKeyCode && Assigns last keystroke to Lastchar
       ENDIF
    
    

  5. Run the form, and type the following into the text box:

          van camp
    

    Note that the V and C are automatically capitalized.

  6. Backspace over the whole name, and start typing another name. Note that the first character is automatically capitalized as a new name is typed.


Additional reference words: 3.00 3.00b VFoxWin
KBCategory: kbtool kbhowto kbcode
KBSubcategory: FxtoolFormdes



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 1, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.