We help IT Professionals succeed at work.

MaxLength

I am building a UserControl which contains a textbox.  I want to limit the number of characters entered into the textbox.  However, I am unable to enforce the Maxlength property of the textbox in the UserControl.  

The MaxLength property can be set and retrieved successfully at run-time.

Any Suggestions?
Comment
Watch Question

Commented:
i am using this for a combo box, but you can try to use it for textbox .


you need to replace
cb_limittext with
Const EM_SETLIMITTEXT = &HC5 or
Const EM_LIMITTEXT = &HC5

p.s i am calling SetComboMaxLength on usercontrol_show event

Unlike the TextBox control, the ComboBox control doesn't expose any MaxLength property, so you have no means of limiting the numbers of characters typed by the end user in the edit area. However you can set this value by sending a CB_LIMITTEXT message to the control, passing the maximum number of characters in the wParam argument. The following code encapsulates this feature in a reausable routine:

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
Const CB_LIMITTEXT = &H141

' Set the maximum number of characters that can be entered in a ComboBox control

Sub SetComboMaxLength(ComboBox As ComboBox, ByVal lMaxLength As Long)
    SendMessageLong ComboBox.hWnd, CB_LIMITTEXT, lMaxLength, Byval 0&)
End Sub

Commented:
Why are you unable to enforce the MaxLength?
Did you create a "pass-through" property?
It seems to work for me just fine!

Author

Commented:
Ok, Im stumped....I add the usercontrol to a new project and it works fine as well.  Still doesn't work in my original app, though...

Thanks

Author

Commented:
Ok, Im stumped....I add the usercontrol to a new project and it works fine as well.  Still doesn't work in my original app, though...

Thanks

Commented:
Try this

Text1.MaxLength = 0   ' set to 0 length first and then set
Text1.MaxLength = 5   ' your length
Commented:
If you continue to have trouble finding the problem, it may be easier to rebuild your control by copying/pasting code from the old one.

When the function fails, it should be easy to trace down what's causing the problem.

Meanwhile, I'd set various debugging points to determine where the setting goes astray or why it's not getting set as expected.

Commented:
One method I've always employed, especially if I'm already writing a routine to govern the type of characters entered (i.e. - numeric only), is place this routine in the KeyPress event of your control.  Let's assume you have a textbox named, "Text1", and you want to limit the the user to 5 characters.

Private Sub Text1_KeyPress(KeyAscii As Integer)
     If Len(Text1.Text) = 5 Then
         KeyAscii = 0
         Beep
     End If
End Sub

-----------------------------------
CODE DESCRIPTION:

          If Len(Text1.Text) = 5 Then

- This checks to see if there are already 5 characters present.  If so...

          KeyAscii = 0
- Cancels character input.


          Beep
- Audibly alerts the user that the control's character limit has been reached.


Commented:

Commented:
As I was trying to say, CArnold, your solution is good, but has many problems.  There's nothing wrong with your code, but users can use a mouse to paste code and the KeyPress event will never fire.  You could try to handle this in the Change event procedure, but that's a royal pain because you don't know what was in the box before the paste (unless you explicitly track it.)

For example,  a user copies the text "myname" into the clipboard, then goes into the textbox where it contains "123" with the cursoe just after the two.  Using the mouse, right-click and select paste, and the box will now contain "12myname3" and totally ignore the 5-character limit.  If you trap this in the Change procedure, should you simply use the left five characters ("12myn") or should you do what you would do when typing and limit it to "12my3" and ignore any extra characters after the max-5 limit is reached?

Really, the answer is to make the control do what it's supposed to do and the problem goes away, along with any future potential problems related to pasting.

Commented:
rspahitz,
   I hadn't considered the user pasting.  Your comment has made me reconsider other alternatives to handling this problem.
Point well made.
   

Author

Commented:
I was unable to trace down the source of the problem.  However, rebuilding the control from the ground up did solve my problem.  Thanks

Commented:
It's obviously a pain to do that, but at least you got it working now!  Glad to help.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.