How to limit the text entered into an edit/richedit control?

Call CEdit::SetLimitText or send EM_LIMITTEXT windows message to an edit control. There are some caveats associated with this functionality…

  1. This limitation does not affect the amount of text entered via a call to SetWindowText, or WM_SETTEXT.
  2. This limitation does not hold true for paste operation.
  3. This limitation does not affect text already entered into an edit control.

If above cases are true then the user is allowed to edit the text but not allowed to insert anymore unless the text count falls below allowed count.

Default limit is 32,767 characters.

[sourcecode language=’cpp’]extern CEdit* pSomeEdit;
pSomeEdit->SetLimitText( 10 );// Only 10 characters can be entered

pSomeEdit->SendMessage(EM_LIMITTEXT, 10, 0); // Using windows message[/sourcecode]

There is another version called EM_EXLIMITTEXT for rich edit controls which supports lengths above 64k.

Appreciate your comments...