Shutting down a DoModal dialog

Posted on 2003-03-31
Medium Priority
Last Modified: 2013-11-20
How can I get a DoModal dialog to shut down by itself ?  I have a modal dialog that I open up for the user to provide his login and password.  Sometimes these are stored in a file, and in that case, I just want the dialog to fill it in by itself and close itself down.

However, if I use OnOK(), EndDialog() or DestroyWindow, I get a crash.

Any ideas?

Question by:Claus
LVL 48

Expert Comment

ID: 8238608
Call CDialog::OnOK() or CDialog::OnCancel(). If this doesn't work in OnInitDialog, set timer in OnInitDialog and close dialog in OnTimer function:

BOOL CMyDlg::OnInitDialog()
    // ...

    if ( ... )    // auto-close needed
        SetTimer(ID_TIMER, 20, NULL);

    return TRUE;

void CMyDlg::OnTimer(UINT nIDEvent)
    if ( nIDEvent == ID_TIMER )
        KillTimer(ID_TIMER);            // we don't need it more

LVL 31

Accepted Solution

Zoppo earned 500 total points
ID: 8238624
It should be possible to call EndDialog() from with OnInitDialog()
without crash ... I used this often without problems.

LVL 12

Expert Comment

ID: 8239835

 If I was you I would either:

  1) Don't display the dialog at all and log the user in automatically. If name and pw are on file login silently.

  2) Leave the Dialog up, file in the name and password but allow the user to hit the 'Login' button.

  Flashing a dialog for an instance can be disconcerting.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Integration Management Part 2
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question