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…
- This limitation does not affect the amount of text entered via a call to SetWindowText, or WM_SETTEXT.
- This limitation does not hold true for paste operation.
- 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.
extern CEdit* pSomeEdit; pSomeEdit->SetLimitText( 10 );// Only 10 characters can be entered pSomeEdit->SendMessage(EM_LIMITTEXT, 10, 0); // Using windows message
There is another version called EM_EXLIMITTEXT for rich edit controls which supports lengths above 64k.