[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 862
  • Last Modified:

MFC CEdit control input restriction

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.

Thanks!  
0
xxg4813
Asked:
xxg4813
  • 3
  • 3
1 Solution
 
nonubikCommented:
You can forbid WM_KEYDOWN events on it except when VK_DEL and the whole text is selected.
0
 
rcarlanCommented:
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.

Radu
0
 
nonubikCommented:
>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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rcarlanCommented:
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.

Radu
0
 
xxg4813Author Commented:
Radu,

Could you give more details on how to use CRichEditCtrl?

Sample code will be appreciated.

Thanks.
0
 
rcarlanCommented:
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));
m_CtrlEdit.SetSelectionCharFormat(tFormat);

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

Radu
0
 
nonubikCommented:
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now