C++ MFC Button on a second Dialog does nothing - c++

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);
}

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

Related

How to insert more dialogs in MFC Dialog application?

I am trying to build an MFC application Dialog based application. It runs ok. But I need to insert another Dialog. So how can I for example, pressing on a button from the first dialog to open the new added dialog?.
I am using Microsoft Visual Studio 2015.
I right clicked on the resources folder and insert a dialog.
It is inserted, but how to create it?.
Thank you.
The easiest way is: I consider you are creating a Dialog based application so you get a main Dialog box and an About Dialog box when Selecting menu->About.
To add Another Dialog to your application:
1- Right click on the solution explorer on the resources files and select Add->Resource->Dialog->New
You get a new Dialog right front of you. Right click on this Dialog and select Add Class. give it for example a name like "MyDlg2" and click ok.
You'll see two files added: MyDlg2.h and MyDlg2.cpp.
Now How to Popup this second dialog (MyDlg2)? Let's create a button on the main Dialog:
Drag a button onto Main Dialog.
Give it a caption "Gong to Dialog2..."
Double-click this button to add a handler for it.
In this handler enter:
MyDlg2 dlg;
dlg.DoModal();
Scroll to the top of this file and add:
#include "MyDlg2.h"
This is important so that main Dialog knows How to create dialog 2.
Build and run.
You need to derive a class from CDialog.
For more information check this MSDN example.

MFC menu item doesn't open dialog box

I am having a proglem with the MFC application and the DialogBox. I am quiet sure I've done everything well with this tutorial: https://msdn.microsoft.com/en-us/library/6wb9s9ah.aspx
but still it doesn't work...
1. I've created new project with simple menu commands.
2. I've created new menu item (+ID) and new resource DialogBox (+ID).
3. Then I've added a new class named CParameters with the Class Wizard. For the BaseClass I've typed in CDialog.
4. I've created new handler on the menu item and added the code
CParameters dlg;
dlg.DoModal();
I think this is it, and this should work... But it doesnt... What is missing??
Here is my project, you can access it freely:
https://www.dropbox.com/sh/e6ajoxqk76hkuvn/AACRMY8bgcuyXguFwP240QB9a?dl=0
Additionally I want to insert TextEditors and to change parameters in my program from the dialog box.
A scan of your source code reveals that you are trying to handle the menu item event within the class that is going to display the dialog.
void CParameters::OnParam()
{
// TODO: Add your command handler code here
CParameters dlg;
dlg.DoModal();
}
I don't see anywhere else that you actually instantiate the dialog class (I may have missed it). What you are trying is incorrect. You cannot handle the menu item event within the same class that displays the dialog because that class (CParameters) has not been instantiated, so, it cannot respond to the menu event. Typically, the menu event would be handled in the mainframe class.
If you are doing this by adding a new menu item from a simple SDI application, then trying adding that part of code in
CMainFrame::OnEdit
OnEdit method used here is obtained from the Event Handler for the new menu item and Message type being COMMAND.

extra initialization in new window of a MFC/OpenCV project

I have a computer vision project using OpenCV and MFC for the GUI.
I want to do the following: when i click Button1, i get a new window that displays a video then captures images by clicking on the buttons of that new window.
First, i had an MFC project with only the window that displays the video and it worked fine. Then i created a new project where i made that window comes after clicking on a button. Here is the code that i used to call that window.
void ClassTestDlg::OnBnClickedButton1()
{
CDialog ClassTestDlg(IDD_DIALOG_WindowDisplay);
ClassTestDlg.DoModal();
}
After clicking on button1, i can see the new window but the video doesn't display.
The class of the new dialog doesn't contain an "OnInitDialog()" method and i don't know how to write extra initialization in this new class.
I am new to MFC so Help please.
Thank you
You cannot instantiate a CDialog, only a class derived from CDialog.
Your button handler is in class ClassTestDlg, so why are you trying to create another ClassTestDlg? You need something like this:
void ClassFirstDlg::OnBnClickedButton1()
{
ClassSecondDlg dlg2;
dlg2.DoModal();
}

QRadioButton not Resetting to unchecked

I have an QDialog class say 'OptionsClass' to display a dialog for options for my Application.
I have designed it in Qt Designer & the object of that class is created in the constructor of my QMainWindow inherited class by new & it is deleted in the destructor (I think this helps in quickly loading the Dialog when button is clicked). Whenever the options button is clicked I am calling a function in OptionsClass which basically edits some text in QLabel & after that calling show(). There are 3 QRadioButton's also in the QDialog class.
When I open the dialog for the 1st time in my application's startup the radio button's are unchecked. Now say i check any button & close the dialog. Now if I again open the Dialog then still that radio button is checked. I want that everytime I open the Dialog all the radio button's should be unchecked.
Here's the SLOT for the button which is clicked to open the Dialog:
void MyMainWindow::on_actionCut_triggered()
{
optionsObj->init(n, 'x');
optioobjn->show();
}
Here is a snippet of the function init:
void OptionsClass::init(int n, char c)
{
//some settings to edit the QLabel
ui->radio1->setChecked(false);
ui->radio2->setChecked(false);
ui->radio3->setChecked(false);
}
I have tried with other properties like SetDown(), SetChecked(), etc but still it doesnt work. What am I doing wrong?
In order to prevent your button from resetting, you need to do
radio-> setAutoExclusive(false);
Then you can uncheck. Don't forget to turn autoExclusive on again.
AutoExclusive is normally off for other abstract buttons, but on for Radio buttons.

Not able to Show a Dialog Box in its class using SW_SHOW in MFC?

I am trying to create a wizard like structure using dialog boxes...So I replaced the code in CDialog1App as below
CDialog1Dlg* dlg = new CDialog1Dlg;
m_pMainWnd = dlg;
dlg->Create(IDD_DIALOG1);
dlg->ShowWindow(SW_SHOW);
the above worked fine...its displying the dialog box.but I have added another dialog box...
So in the first dialog box if the user clicks Next it has to hide the first dialog box and display the second dialog..
//CDialog1 class
void CDialog1Dlg::OnBnClickedNext()
{
// TODO: Add your control notification handler code here
CDialog2* dialog2 = new CDialog2();
dialog2->Create(IDD_DIALOG2);
dialog2->ShowWindow(SW_SHOW);
this->ShowWindow(SW_HIDE);
}
in the above code am creating an object for the Dialog2 class and trying to show that....
Now the problem is,when I click next its hiding both the windows..What can I do..I tried several types but its still its not workin..Please dont suggest me to do with PropertySheet..It will work with that, i know ...but I want this using Dialog Box for some reason
You're creating the dialog2 with the default parent window (NULL):
dialog2->Create(IDD_DIALOG2);
But the default parent seems to be dialog1 in your case. And since you hide dialog1 which is the parent of dialog2, dialog2 is also hidden.
Find the window (CWnd) of either your main app dialog (if you have one visible apart from your wizard), or use the desktop window as the parent.
For example:
dialog2->Create(IDD_DIALOG2, GetDesktopWindow());