I have a DB text field, size 500, linked to a MFC dialog field using a CRichEditCtrl
. I call CRichEditCtrl::LimitText(500)
so you can't enter more text than the DB can handle and this works just fine, until you enter new-lines. Each new-line increases the length of the string by 2, but only counts as 1 towards the 500 limit... e.g. if I fill the edit control to the limit and have 4 new-lines, the CString
has length 504.
I specifically want the control to only let the user enter text up to the 500 char limit, rather than truncate what they enter. How can I best achieve this? Note, I need to save the contents of the CString
as-is, no replacing \r\n
with \n
in the DB or anything like that.
Overwrite OnChar
, count the number of new-lines in the text and then set ctrl.LimitText(500 - count)
. This way you avoid busting the DBs 500 char limit.
You also have to watch for paste events which could break the limit, as described here: How does a CRichEditCtrl know a paste operation has been performed?