Click to See Complete Forum and Search --> : knowing when user select OK in modal dialog box


Danielle Harvey
April 8th, 1999, 09:19 PM
I created a menu item for a modal dialog box and eventually use the code:

void CMainFrame::OnDialogsModal()
{
CModal modalDlg;
modalDlg.DoModal();
}

Everything works great. Now I want to print to the SDI screen when the user selected the OK button or when the user selected the CANCEL button. Reading through books, I employ the following code in the view class;

void CTextEditView::OnDraw(CDC *pDC)
{
CModal modalDlg;
if (modalDlg.DoModal() == IDOK)
pDC->TextOut(1,1,"user pressed ok");
if (modalDlg.DoModal() == IDCANCEL)
pDC->TextOut(1,1,"user pressed cancel");
}

When I add the previous code, my dialog box appears when I execute the program rather than when I select the menu item! Furthermore, the dialog box will sometimes keep appearing even after I hit the OK button. How can I fix this so that the dialog appears only when I select that menu item and when I hit OK, it prints to the screen "user pressed ok" and then the modal dialog box goes away until I call it again?

Any response any one can give me will be greatly appreciated.

Daniel Levine
April 8th, 1999, 09:43 PM
Hi,

The reason the dialog appears when you start the program is that you placed the code in the wrong place. Whenever the document is sent a draw message, your dialog is shown. You should move the code to a message handler through ClassWizard. The reason the dialog appears twice when you press OK is that you are calling modalDlg.DoModal() twice. Once to check for OK, and once to check for Cancel. You should change your code to:

CModal modalDlg;

int ret = modalDlg.DoModal();
if (ret==IDOK)
...
else
...

Daniel.

Danielle Harvey
April 10th, 1999, 08:25 AM
If I create the message handler

void CModal::OnOK()
{
CModal modalDlg;
if (modalDlg.DoModal() == IDOK)
{ ??? }
}

How can I then print something to the SDI screen? Previously in the View class, I used pDC->TextOut(1,1,"ok"); but I can't this to work if I put it under the message handler. Please, any response you can give me will be greatly appreciated.

Fabian
April 10th, 1999, 10:15 AM
Hi, a couple of things
You should't call a dialog box from OnDraw, because each time the view is redrawn you'll have the dialog box poping up, and this can be really annoying.
The other thing is, if you map the OnOK in your dialog call you don't need check for DoModal() == IDOK, not mentioning your code is wrong
void CModal::OnOK()
{
CModal modalDlg;
if (modalDlg.DoModal() == IDOK)
{ ??? }
}


this mean you are creating a new modal dialog when you select ok in the first one wherever you had created it.

So I suggest you, choose other place to call your dialog, not OnDraw, and you can create a CClientDC to print out your text, something like this

void CTextEditView::Whatever()
{
CClientDC cDC(this);
CModal modalDlg;
if (modalDlg.DoModal() == IDOK)
cDC.TextOut(1,1,"user pressed ok");
else
cDC.TextOut(1,1,"user pressed cancel");
}



I hope this help.

Fabian