MFC CEdit control input restriction

Posted on 2005-04-26
Last Modified: 2007-12-19
I have a CEdit control which is used to let users define an email template. In the email body, users can choose some predifined fields from a set, like date, time, ... If a user chooses date, the CEdit control will show {date}. When the email is sent, the {date} will be replaced by actual date value, eg. 2005/04/26.

My question is: how can I forbid a user from changing these selected fields? He can only delete it as a whole but cannot add or delete any single character in tihs field.

Question by:xxg4813
    LVL 16

    Expert Comment

    You can forbid WM_KEYDOWN events on it except when VK_DEL and the whole text is selected.
    LVL 9

    Expert Comment

    You could indeed try to handle keyboard events manually (you'll also have to take care of the context menu Paste and Delete commands, as well as corresponding keyboard shortcuts), or alternatively you could switch to using a CRichEditCtlr instead of a CEdit. In rich edit controls you can mark paragraphs as read-only using the CFE_PROTECTED flag. Once a paragraph is marked read-only, the control will take care of all the "low-level" work.

    LVL 16

    Expert Comment

    >Once a paragraph is marked read-only, the control will take care of all the "low-level" work.
    But the user still needs to delete it.
    LVL 9

    Expert Comment

    Good point, nonubik.

    With protected paragraphs in rich edit controls you can also register for notifications by specifying the ENM_PROTECTED flag. The control will then send EN_PROTECTED notification messages whenever the user tries to change something in a protected paragraph. The notification message contains details about the original user input that triggered the event.

    Of course, at this point you're back to doing the "low-level" stuff yourself.

    LVL 4

    Author Comment


    Could you give more details on how to use CRichEditCtrl?

    Sample code will be appreciated.

    LVL 9

    Accepted Solution

    Instead of attaching a CEdit to your dialog control ID, you attach a CRichEditCtrl object. Like so:

    CRichEditCtrl m_CtrlEdit;  // in your CDialog derived class

    You can do this through the Class Wizard.

    If you do it through Class Wizard, you'll also get the corresponding:

    DDX_Control(pDX, IDC_YOUR_EDIT, m_CtrlEdit);

    in the DoDataExchange method.

    With these in place, you can initialise the rich edit control with text pretty much like a normal edit box and you can also use it like one (i.e. extract text from it).
    However, to write-protect paragraphs you have to do a bit more work. For example:

    CHARFORMAT tFormat;
    tFormat.cbSize = sizeof(tFormat);
    tFormat.dwMask = CFM_FACE | CFM_SIZE | CFM_PROTECTED;
    tFormat.dwEffects = CFE_PROTECTED;
    tFormat.yHeight = 8 * 20;      // 8 point
    _tcscpy(tFormat.szFaceName, _T("Courier New"));

    // make first two lines read-only
    m_CtrlEdit.SetSel(0, m_CtrlEdit.LineIndex(2));

    You can call SetSelectionCharFormat any number of times for different sections of text in the control.

    LVL 16

    Expert Comment

    In your InitInstance method of the CWinApp derived class you *need* to call AfxInitRichEdit or AfxInitRichEdit2 depending on whether you use rich edit 1 or 2.0 control in order for the application to run properly.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    linearIn  challenge 23 54
    method argument as final 1 54
    changeXy challenge 13 40
    java  and programming certification ? 4 29
    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now