0
votes

It seems like the simplest thing (using Visual Studio):

  • Place an edit control (MFC CEdit) on a dialog
  • Right click and choose "Add variable..."
  • Link the control to a variable (e.g. CString someText)
  • Right click on the control again and choose "Add Event Handler..."
  • Click the "Add and Edit" button so it adds an EN_CHANGE event handler
  • The class wizard generates a handler function like OnEnChangeEdtSomeText()

Terrific - each time a character is typed, the handler function is invoked. However, on all other events I have ever handled with MFC controls, UpdateData() would exchange data between the control and a variable linked to it - the state of the control is reflected in the state of the linked variable after UpdateData() is called. Why not here? Clearly I have to use something else, and so far GetWindowText() seems to work fine for retrieving the text I can see in the CEdit control.

But why has the normal function of UpdateData() been destroyed? For example, if I type "foo" into my edit control, then click or tab away to kill focus, then return to type "bar", on none of the invocations of OnEnChangeEdtSomeText() (all 6 of them) does UpdateData() lead to any data whatsoever in the variable linked to it. Why not? Can I trust EN_CHANGE not to interfere with other data entry? Are there other notifications that will break what I had come to believe was the normal link between a control and a variable linked through a DDX... mechanism?

1
"But how does one actually get the character just typed?" - That's a wrong assumption. EN_CHANGE can be raised, when the user didn't type a character (e.g. pasting from the clipboard with the mouse). It's not clear, what problem you are really trying to solve. This question is asking about your proposed solution only. - IInspectable
You are right - that was a rhetorical question that I have edited out. Hopefully the question is clearer now. - omatai
You can trace into UpdateData to see exactly what it's doing and why it's failing. But calling UpdateData on every character input is an abuse of the system - even if you get it working, I would recommend using GetWindowText as you're currently doing. - Mark Ransom
Clearly UpdateData() is not the right thing to use in this particular context - that's fine. But I'm really surprised that it seems to break completely as if EN_CHANGE is some kind of kryptonite to it. Since UpdateData() is used so liberally in the software I'm using, I'd like to understand what hidden traps there might be with it. - omatai
@omatai I just tried exactly what you have described, and it works just fine here. Without seeing your actual source code (or at least the relevant parts suche as the .h file and the DoDataExchange function) it's somewhat hard to help. - Jabberwocky

1 Answers

0
votes

If it's a rich edit, you need to call SetEventMask(ENM_CHANGE) on it, or the message will not be sent on edition.