Ceph
Ceph

Reputation: 161

C++ MFC Button on a second Dialog does nothing

I created a C++ MFC Program with the Visual Studio Wizard. There I set the application type to "Dialog Based".

I have a button on the first dialog, which opens another dialog. I created this second dialog by right clicking on the project -> Add -> Resource -> Dialog -> New. Then I added a MFC class to the new dialog by double clicking it in resource view.

On the second Dialog I also created a button. I double clicked the button and added some code which should be executed.

When I run the program and click the button on the second dialog, nothing happens and the code is not executed. I did everything like with the button on the first dialog. That one works fine. Why is the second button not working and what do I need to do?

First Dialog

void CMFCApplication1Dlg::OnBnClickedButton1()
{
    CDialogEx secondDialog(IDD_DIALOG1);
    secondDialog.DoModal();
}

Second Dialog

void SettingsDlg::OnBnClickedButton1()
{
    MessageBox(L"Button clicked", L"Button clicked", MB_OK);
}

Upvotes: 1

Views: 569

Answers (1)

Ceph
Ceph

Reputation: 161

@andrew-truckle, your side node was the answer! I changed it to:

void CMFCApplication1Dlg::OnBnClickedButton1()
{
    SettingsDlg settingsDialog;
    
    settingsDialog.DoModal();
}

Now the button works just as expected. Thank you very much!


Further Info (from @andrew-truckle)

For the benefit of others the issue here was that the original code declared the dialog like this:

CDialogEx secondDialog(IDD_DIALOG1);

That was wrong because the dialog was actually associated with the class SettingsDlg. This is the class that had the message map and event handlers etc. CDialogEx was the base class.

I added this update to the answer to save the reader from locating my comment to the question.

Upvotes: 3

Related Questions