Home > MFC, Windows API > How to limit the text entered into an edit/richedit control?

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.

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.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: