How to Limit User Input in VB Combo Box with SendMessage API

Last reviewed: June 21, 1995
Article ID: Q72677
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0

SUMMARY

You can specify a limit to the amount of text that can be entered into a combo box by calling SendMessage (a Windows API function) with the EM_LIMITTEXT constant.

MORE INFORMATION

The following method can be used to limit the length of a string entered into a combo box. Check the length of a string inside a KeyPress event for the control, if the length is over a specified amount, then the formal argument parameter KeyAscii will be set to zero.

Or, the preferred method of performing this type of functionality is to use the SendMessage API function call. After you set the focus to the desired edit control, you must send a message to the window's message queue that will reset the text limit for the control. The argument EM_LIMITTEXT, as the second parameter to SendMessage, will set the desired text limit based on the value specified by the third arguments. The SendMessage function requires the following parameters for setting the text limit:

   SendMessage (hWnd%,EM_LIMITTEXT, wParam%, lParam)

   wParam%   Specifies the maximum number of bytes that can be
             entered. If the user attempts to enter more characters,
             the edit control beeps and does not accept the characters.
             If the wParam parameter is zero, no limit is imposed on
             the size of the text (until no more memory is available).

   lParam    Is not used.

The following steps can be used to implement this method:

  1. Create a form called Form1.

  2. Add a combo box called Combo1 to Form1.

  3. Add the following code to the general declarations section of Form1:

        '*** Note: Each Declare statement must be on just one line:
    

        Declare Function GetFocus% Lib "user" ()
        Declare Function SendMessage& Lib "user" (ByVal hWnd%,
    
                                                  ByVal wMsg%,
                                                  ByVal wParam%,
                                                  lp As Any)
        Const WM_USER = &H400
        Const EM_LIMITTEXT = WM_USER + 21
    
    

  4. Add the following code to the Form_Load event procedure:

        Sub Form_Load ()
    
           Form1.Show            ' Must show form to work on it.
           Combo1.SetFocus       ' Set the focus to the list box.
           cbhWnd% = GetFocus()  ' Get the handle to the list box.
           TextLimit% = 5        ' Specify the largest string.
           retVal& = SendMessage(cbhWnd%, EM_LIMITTEXT, TextLimit%, 0)
        End Sub
    
    

  5. Run the program and enter some text into the combo box. You will notice that you will only be able to enter a string of five characters into the combo box.


Additional reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: APrgOther


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