1
votes

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);
}
1
Mandatory reading: Message Maps (MFC). That not only explains how to do it properly, but also puts you in a position to understand that the code provided is insufficient to answer the question.IInspectable
Double-clicking a control on a dialog resource is a perfectly acceptable way to create a click event handler and the system should add all the boiler place code. Make sure your message map entry is also there (higher up in the class file).Andrew Truckle
Side note: You declare your variable as CDialogEx and your dialog is actually a SettingsDlg class (most likely derived from CDialogEx). So why not declare your variable as the right type of object ...?Andrew Truckle

1 Answers

3
votes

@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.