How to Limit the Number of Characters Typed in a Combo Box

Last reviewed: October 25, 1995
Article ID: Q138499
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 3.0

SUMMARY

You can limit the number of characters that a user can type into a text box by using the InputMask property. However, this property does not exist for a combo box. This article shows shows by example how to limit the number of characters typed in a combo box by placing code in the KeyPress event.

MORE INFORMATION

Step-by-Step Example

  1. Create a new form.

  2. On the Form menu, click New Property, and name the new property numb.

  3. Place a combo box on the form, and set its ControlSource, RowSourceType, and RowSource properties.

  4. Place at least one other control on the form.

  5. Place the following code in the KeyPress event for the combo box to limit the number of characters entered into the combo box to 3:

       IF nKeyCode=13 OR nKeyCode=9   && test for ENTER or TAB
         THISFORM.numb=0              && and reset numb
       ELSE
         IF !(nkeyCode=127 OR ;       &&  BACKSPACE
           nkeyCode=7 OR ;            &&  DELETE
           nkeyCode=6 OR ;            &&  END key
           nkeyCode=19 OR ;           &&  LEFT ARROW
           nkeyCode=4 OR ;            &&  RIGHT ARROW
           nkeyCode=1)                &&  HOME key
           THISFORM.numb=THISFORM.numb+1  && if not one of the above increment
    
             IF THISFORM.numb=4       && When 4 characters have been entered
               THISFORM.numb=0        && reset numb to zero and flash a message
               ?? CHR(7)              && ring the bell
               =MESSAGEBOX("Enter only 3 characters")
               NODEFAULT              &&  do not enter the character typed.
             ENDIF
         ENDIF
       ENDIF
    


Additional reference words: 3.00 VFoxWin mask format
KBCategory: kbprg kbcode kbhowto
KBSubcategory: FxotherGeneral


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: October 25, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.