Andres
Andres

Reputation: 3414

How to implement the event handler for a MFC CEdit ON_EN_SETFOCUS?

I'm just maintaining some MFC C++ code and I have a screen with many CEdit objects.

I want to implement the OnFocus event for all of then but write only one method to treat the event.

To do that I would need to know the CEdit ID that fired the event but it looks like the implementation of the OnFocus event in MFC does not have this as a parameter (compared to some other events like OnCtlColor that has CWnd* object as a parameter).

I just refuse to believe that I have to implement a small method for each single CEdit passing its ID to the main method that will do what I want!. If that's the only solution, shame on you MFC!

Upvotes: 0

Views: 2608

Answers (1)

Zdeslav Vojkovic
Zdeslav Vojkovic

Reputation: 14591

ON_CONTROL_RANGE macro exists exactly to allow mapping single handlers to the same event on multiple controls.

First you need to ensure that your control IDs are sequential. Then in the header you need to declare a handler which takes the control ID as a parameter:

afx_msg void OnSetFocusMulti(UINT ctrlId);

This allows you to distinguish the sender control in your handler if you need it.

Now you need to add this to message map, instead of bunch of ON_EN_SETFOCUS(IDC_EDIT1, &CMyDlg::OnSetfocus):

ON_CONTROL_RANGE(EN_SETFOCUS, IDC_EDIT1, IDC_EDIT_X, OnEnSetFocusMulti)
                      ^           ^          ^             ^
//       notification code | first ctrl | last ctrl |   the handler

Other message map macros are documented here

Upvotes: 3

Related Questions