Conditionally stopping a CPropertySheet from closing from the page OnOK button handler

519 views Asked by At

I have just encountered an issue with CPropertyPage.

I have been trying to use the OnOK handler to do some validation:

void CCalendarSettingsGooglePage::OnOK()
{
    bool bHandle = false;

    UpdateData(TRUE);

    // AJT v20.2.0 — We need to pass "true" so that the error message will display!
    if (ValidSettings(true))
    {
        bHandle = true;
        SaveSettings();
    }

    if (bHandle)
        CMFCPropertyPage::OnOK();
}

The problem is, the sheet still closes down. I had hoped that preventing the CMFCPropertyPage::OnOK would have stopped the sheet closing. But it doesn't.

I understand from here that the sheet's OnOK is making a EndDialog(IDOK) call. But i don't want to complicate my sheet. The testing is here in this page. so I need a was for the sheet to know if it should close or not when user clicks the OK button.

1

There are 1 answers

2
Adrian Mole On BEST ANSWER

You need to override the OnCommand handler of your property page's parent property sheet class and intercept the clicks for the IDOK command (which will be given in the wParam parameter). If you do not call the base class OnCommand but still return TRUE to indicate that your have handled the command, then the property sheet will not close:

BOOL MyPropertySheet::OnCommand(WPARAM wParam, LPARAM lParam)
{
    if (wParam == IDOK) { // OK button clicked...
        if (!ValidSettings(true)) return TRUE; // NOT valid, prevent further processing.
    }
    // You can also intercept the "Apply" command by testing for ID_APPLY_NOW

    // Everything is OK, so continue processing ...
    return CMFCPropertySheet::OnCommand(wParam, lParam);
}

Note that I have assumed your parent is derived from CMFCPropertySheet, but the same works for the 'older' CPropertySheet.