Subscribe to RSS

PropertySheet DoModal caveat

Back to MainPage.  Back to ProgrammingStuff.

A little MFC tip this afternoon.  This applies to CPropertySheet, and CPropertyPages.

Say you've got a property page CMyPage : CPropertyPage, and you want to display it.  Easy, of course.  Just wrap a default CPropertySheet around it (and any other pages you want to display), and call DoModal.

Except, unless you specify the parent window for the new property sheet, you'll still be able to manipulate the UI of other windows in your app, essentially causing the call to DoModal, to act modelessly.  For your CPropertySheet to act modally, ensure pParentWnd is not null.  (pParentWnd is a CWnd *).

        CMyPropPage page1(config);
CPropertySheet sheet(_T("My PropSheet"), pParentWnd);
sheet.AddPage( &dlg );
sheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
if (IDOK == sheet.DoModal())
{ ...

This isn't a big problem, but might help someone save a few minutes of head scratching.

I hit this problem because I started my development by calling my PropertyPage from a test harness function, using rundll32.  This showed only the sheet I was working on, which is great for speed of development, but caused me to not catch this bug until launched from it's calling UIs.

Another tip:  If you don't want to present an Apply button to the user, add the PSH_NOAPPLYNOW flag to the member dwFlags in the property sheet's config structure m_psh, as shown in the above snippet.

Back to MainPage.  Back to ProgrammingStuff.

Edited on Fri, Apr 9, 2010 at 6:29 a.m.