MFC dialog box continue after pressing ok - c++

I have a custom dialog message box that pops-up when a edit control in my main dialog has wrong data.
CDlgError dlgError = new CDlgError(this);
dlgError.Create(CDlgError::IDD, this);
dlgError.m_staticMessage.SetWindowTextA("Error message!");
dlgError.ShowWindow(SW_SHOW);
//more code
I want the rest of the code to be executed only after i press an OK button in my CDlgError pop-up dialog. How can i do that?

Use DoModal instead of Create and ShowWindow to show your error dialog. e.g.
CDlgError dlgError = new CDlgError(this);
dlgError.m_strMessage = "Error message!";
dlgError.DoModal();
As you can see from the code you'll need to pass in the text and THEN set your message label inside CDlgError::OnInitDialog because the control won't be initialized before going modal.

You are creating a dialog using Create which shows a modalless dialog(you can click on other parts of application even dialog is open).
You requirement is for modal dialog where you can not click on any part of application until this dialog is closed.
To do this use DoModal function instead of create.

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.

C++ MFC create new dialog and add Combobox item

I'm using MFC to build an application with two dialogs.
When I press a button in the parent dialog a new window including a Combobox should apprear.
I created a first dialog with a button "New". This button will open the second dialog.
Therefor I created a second dialog with a Combobox. The Combobox has a linked variable variableCombobox. The second class is called CSecond.
Before I do anything in the new dialog I want to add an item to the Combobox.
In the first dialog class I create the new window like this:
void CFirstDlg::OnBnClickedNew()
{
CSecond dlg2 = new CSecond();
dlg2.variableCombobox.AddString(L"test");
dlg2.DoModal();
}
The program crashes in the line I want to add the test string to the Combobox showing an assertion error.
I noticed that the dlg2 object is null but I don't know why.
Can anyone tell me how to create a second window immediately adding an new item in the Combobox of the second window?
The problem is the second dialog is a modal dialog. The windows does not exist before the call to DoModal() and no longer exist after that function returns. Therefore calling AddString on the combobox is not all right, since the combo box does not exist at that time.
The solution is to initialize the dialog with your desired values (like in the constructor for instance, or other methods) and then in OnInitDialog() use those values to setup the controls (including this call to AddString for the combobox).

How to call a dialog box from another (Win32 application)

I implemented a dialog-based Win32 Visual C++ application (Visual Studio Ultimate 2012) by following this article.
What is the way to call another dialog box (by clicking on a button) from the one I already created?
Add a button to the dialog in the dialog resource view. Just drag a button from the toolbar onto the dialog template. When the button is clicked you will get a WM_COMMAND message containing the button ID and the BN_CLICKED notification code.
http://msdn.microsoft.com/en-us/library/windows/desktop/bb761825(v=vs.85).aspx
Add a case in your DialogProc to detect the click. When you get it, create a new dialog by calling the DialogBox API.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms645452(v=vs.85).aspx
This second dialog will need you to write a new DialogProc2, just like the first DialogProc, to handle messages from the second dialog.

How to minimize the page in the property sheet?

I have used property sheet concept and display 3 dialog box on the property sheet.
Now to access the each dialog i have to clicked on the tab of each dialog.
But my need is that i want to minimize the first dialog and show the second dialog without using tab. for that i have added one button using that button i minimized the first dialog but when i tried to call the second dialog i get assertion failed error. i have done like this...
In the 1st dialog (.cpp) file :
void ClstdemoDlg::OnBnClickedMinimize()
{
ClstdemoDlg_1 dlg; // created 2nd dialog variable.
this->ShowWindow(SW_MINIMIZE); // minimize the 1st dialog
dlg.ShowWindow(SW_SHOW); //Tried to show 2nd dialog but get Assertion failed Error
}
Please let me know how to do this?

How to hide a modal dialogbox in MFC application?

I have a hard time hiding a modal dialog box. What I am doing is - I am trying to design a UI for my own application in MFC, kind of a setup assistant.
In the 1st dialog box I have NEXT button, so when I click that it has to hide the 1st dialog box and move on to the 2nd dialog box, where I have some controls in 2nd dialog box.
How can I achieve that?
I have never tried to hide a Modal dialog...not sure how it can be done.
Anyway, it seems to me you don't need to hide the dialog but destroy the first one and create the second one. You can use EndDialog to terminate a modal dialog.
But MFC has its own mechanism to create your own wizard, have a look at this class CPropertySheet. I am sure you can find thousand of examples, this is one.
Hope it helps.
You can use ShowWindow() function to hide modal
Its default patametet is SW_SHOW which is equal true value 1 and
To hide modal use SW_HIDE value when you click next button
You just use ShowWindow(SW_HIDE) If you make prev button you should use modal pointer
Or next modal should child modal because you cannot have prev modal variable.
I wish you understand me for my english