Visual Basic Combo API
SendMessage: Set 'MaxLength' in a Combo's Edit Box
     
Posted:   Saturday May 5, 1999
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB5, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Prerequisites
None.

Here is a simple method to limit user input into a Style-0 or Style-1 combo box, using SendMessage and CB_LIMITTEXT.
 BAS Module Code
None.

 Form Code
Toss a Combo (style 0 or style 1) and two command buttons onto a form. Keep the default names, and add following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const CB_LIMITTEXT = &H141

Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long
   
Private Declare Function SetWindowText Lib "user32" _
   Alias "SetWindowTextA" _
  (ByVal hwnd As Long, _
   ByVal lpString As String) As Long



Private Sub Command1_Click()

   Dim maxEditLength As Long
   
  'how many chrs to allow
   maxEditLength = CLng(Text1.Text)
   
  'clear existing text
   Call SetWindowText(Combo1.hwnd, "")
    
  'set the limit
   Call SendMessage(Combo1.hwnd, CB_LIMITTEXT, maxEditLength, ByVal 0&)
   
End Sub


Private Sub Command2_Click()

  'clear existing text
   Call SetWindowText(Combo1.hwnd, "")
    
  'reset to the control default
   Call SendMessage(Combo1.hwnd, CB_LIMITTEXT, 0&, ByVal 0&)
   
End Sub
 Comments
Pressing the Limit button will restrict user input into the Style-0 or Style-1 combo to the value passed as maxEditLength. Passing 0& as wParam, as in the Command2 routine, removes the input restriction.

The MSDN has the following notes on CB_LIMITTEXT:

If the combo box does not have the CBS_AUTOHSCROLL style, setting the text limit to be larger than the size of the edit control has no effect.

The CB_LIMITTEXT message limits only the text the user can enter. It has no effect on any text already in the edit control when the message is sent, nor does it affect the length of the text copied to the edit control when a string in the list box is selected.

The default limit to the text a user can enter in the edit control is 30,000 characters.

The value of wParam specifies the maximum number of characters the user can enter. If this parameter is zero, the text length is set to 0x7FFFFFFE (2147483646) characters.

The return value is always TRUE.


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

 

Hit Counter