Stop a window from closing when close button is clicked on certain exceptional conditions

Ramesh Deekonda 216 Reputation points
2020-10-22T16:41:00.313+00:00

To all,

I have built an application that uses property sheet (with property pages). When OnClose of a property page is called. I try to return from the OnClose before the call to CMFCPropertyPage::OnClose(), To prevent the window from closing.Inspite of this the property page closes.

The actual code in the OnClose function is here :
first case:
void CRunJobPage::OnClose()
{
// TODO: Add your message handler code here and/or call default
extern CString gStrJobName;
extern int GetUpdatedStatus(char* jobname);

if (GetUpdatedStatus(gStrJobName.GetBuffer()) == 0)
{
if (MessageBox("You have made changes to your job\r\n and not committed them. Close Now?", "Choosing Yes will revert your changes\r\n and close, Choose No to go back\r\n and commit your changes", MB_YESNO) == IDYES)
RevertChanges(gStrJobName);
else
{
return;//prevent property page from closing
}
}

AfxMessageBox("In on close");
CMFCPropertyPage::OnClose();//can close if it reaches here
}

second case:
i have tried an alternative strategy by creating a custom button on the property page. I have placed the same code in the OnBnClicked of this button. that when clicked executes the same logic as above.In this case the property page never closes.

In the first case it allways closes and in second case it does not close.
please give your advice on what I could be doing wrong.
Hope you have understood the question otherwise i can clarify based on your response.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,440 questions
{count} votes

Accepted answer
  1. Ramesh Deekonda 216 Reputation points
    2020-10-23T09:51:16.987+00:00

    Solved: In case 1 of my code The ID of the button for OnClose() happened to be IDCANCEL so it was invoking OnBnClickedCancel inside which EndDialog was called and was always closing. In case 2 I put the EndDialog as suggested by you and it works.
    The line I added was
    ((AppPropSheet*)GetParent())->EndDialog(IDCANCEL);
    Thank You.
    @Jeanine Zhang-MSFT Thank you for your response.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful