Jan 232009

Many of you must have faced this obscure problem, it’s because you haven’t called AfxInitRichEdit/AfxInitRichEdit2() function.

Wait! Before you jump onto add this call identify your rich edit control’s class name, if you are working in VC6 then rich edit class name will be RICHEDIT and or if VC7 and above then it will be RichEdit20A. So if the class name is RICHEDIT then call AfxInitRichEdit(), else if the class name is RichEdit20A then call AfxInitRichEdit2().

Aug 292008

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.