Creating event handler dinamically in MFC

Hi!

I would like to ask for some help about MFC programming.

I should create numerous EditBoxes at runtime (not design time) which are CEdit type. My user interface is a CDialog.

So the creating code is the following:

...
1
2
3
4
5
6
7
8
9
10
BOOL CPMM_parameterezoDlg::OnInitDialog()
{
for ( int i = 0; i < 3; i++)
	{
		CEdit *pCEdit = new CEdit();
		pCEdit->Create(WS_CHILD | WS_VISIBLE | WS_BORDER, 
			CRect(10, 50 + i * 40, 130, 70 + i * 40), this, 0x1552 + i);
		editBoxes.push_back(pCEdit);
	}
}

...


(The editBoxies is a vector)

But I don’t know how I can create event handler (for example: OnKeyDown handler) programmatically (at runtime) for these EditBoxes.

Thank you the suggestions!
I've found the solution.

I have to put a new line "ON_CONTROL_RANGE" in the BEGIN_MESSAGE_MAP.

1
2
3
4
5
6
7
8
BEGIN_MESSAGE_MAP(CPMM_parameterezoDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()

	ON_CONTROL_RANGE(EN_CHANGE, IDC_EDIT1, IDC_EDIT6, OnChange)

	//}}AFX_MSG_MAP
END_MESSAGE_MAP()



It is rewarding to create constans in Resource.h:
1
2
#define IDC_EDIT1						10001
#define IDC_EDIT6						10006 


(Here I make 6 piece of CEdit)

And of course the common event handler:

1
2
3
4
5
6
7
void CPMM_parameterezoDlg::OnChange(UINT nID)
{
	if ( nID == IDC_EDIT1 )
	{
		...
	}
}
Topic archived. No new replies allowed.