Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,

I want to make a dialog that get opened only if a condition occurs.
in the dialog, I will have ok button and cancel button, and I want to continue only if user presses OK.

for example:

if (condition)
{
 open dialog.
 if user clicked OK, do something
 else do something else
}


How can I do it using MFC?

Thanks
Posted
Updated 17-Jan-12 2:18am
v2
Comments
Sergey Alexandrovich Kryukov 17-Jan-12 23:41pm    
OK, this is a pseudo-code. Now write some code out of it. You did not explain main thing -- what's the problem?
--SA

In a glance, you need a modal dialog. I think there's a plethora of tutorials[^] on the web.
 
Share this answer
 
This is usually done calling DoModal() of your dialog and using the return value:
C++
if (condition)
{
    CMyDialog Dlg;
    if (Dlg.DoModal() == IDOK)
    {
        // OK pressed
    }
    else
    {
        // Dialog cancelled
    }
}
 
Share this answer
 
Comments
Schehaider_Aymen 17-Jan-12 8:55am    
only extra information, you have to include the .h too

#include "CMyDialog.h"
This is trivial...

If you only need to display information, use a AfxMessageBox() (it's easy to use), if you need a dialog, then make a dialog derived from the CDialog MFC class and use the CDialog::DoModal() method to bring up the dialog (this is if you want to make it modal, to make it modeless, create it first then bring it up using ShowWindow()).

Override CDialog::OnInitDialog() to update any controls you may be using within the dialog (DO NOT do this in the constructor! This is a common mistake, the controls have not been created at this point).
 
Share this answer
 
Hi,

I think you don't need a dialog, instead you only need a MessageBox()

you can use the MessageBox like this;

if(IDOK== MessageBox(""))
{

}
else
{

}

Cheers
 
Share this answer
 
ha,Maybe you will like the API:GetOpenFileName,It is suiteable to you~~
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646927(v=vs.85).aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900