2

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.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164

1 Answers1

2

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • My problem is that I have two sheets, both with this `ValidSettings` method. As it stands I can only call this method for the active page (which I do not know how to detect). When I try to run it for the page (not visible) I get an assertion. I need a way to validate the settings for both pages. For example, if I and on page 1 and go to page 2, and page 1 does not have valid settings, it stop not allow going to page 2. Vice versa. And Ok as above, if we click OK, BUT only validate the active page (somehow). – Andrew Truckle Jun 18 '20 at 11:46
  • At the moment I have added `OnKillActive` to each page. In there it does the validation and flags the user. Then, in the sheets `OnCommand` I just use `GetActivePage` and compare the handles. It then knows which to perform validation on. It working ok! – Andrew Truckle Jun 18 '20 at 12:00