0

I am trying to add a CPropertySheet with three CPropertyPages to my MFC application. My problem is that the Property sheet only shows for less than a second then closes. When I open a different modal dialog after creating the CPropertySheet, the CPropertySheet stays open and I can use it with no problems. Here is my code:

BOOL CSLIMOptCplusplusApp::InitInstance()
{

CWinApp::InitInstance();
SQLHENV m_1;
EnvGetHandle(m_1);


Login lgn;
lgn.DoModal();




CImageSheet*      imagedlg            = new CImageSheet("Image Capture Dialog" );
CImageDisplay*    pageImageDisplay    = new CImageDisplay;
CImageDimensions* pageImageDimensions = new CImageDimensions;
ListOption*       pageListOption      = new ListOption;

ASSERT( imagedlg );
ASSERT( pageImageDisplay );
ASSERT( pageImageDimensions );  
ASSERT( pageListOption );

imagedlg->AddPage( pageListOption);
imagedlg->AddPage( pageImageDimensions );
imagedlg->AddPage( pageImageDisplay );

imagedlg->Create( NULL,
              -1,
              WS_EX_CONTROLPARENT | WS_EX_TOOLWINDOW ); 

imagedlg->ShowWindow( SW_SHOW );

I think my problem may be at imagedlg->Create( when I use NULL as the first parameter. The tutorial I was following used this in place of the NULL. However, that gives the error:

IntelliSense: argument of type "CSLIMOptCplusplusApp *" is incompatible with parameter of type "CWnd *"

I also tried imagedlg->Create(); and it also only flashes for a moment. I would like my CPropertySheet to stay open until it is closed. Thanks for any help!

EDIT: Here is an image of what I wish my property sheet to look like. My first tab used a ListControl to change database options, the other two tabs are going to do other things. My intent is to keep the dialog/propertysheet looking the same as it does now, but to stay open instead of closing. CPropertySheet

Kyle Williamson
  • 2,251
  • 6
  • 43
  • 75
  • 1
    I'm confused by the window styles you're using. What is your intent? – rrirower Mar 13 '15 at 15:15
  • I updated with a picture and a description. My intent is to have three tabs that do three different things in my application. I used a CPropertySheet instead of a TabControl because it allowed me to add designer-made dialogs as pages. – Kyle Williamson Mar 13 '15 at 15:24
  • 1
    So, why the extended window styles? What is the context of the code you've shown? How does it fit into the entire application? – rrirower Mar 13 '15 at 15:28
  • Please forgive me, but I don't know what you mean by the extended window styles I am using. The code I have shown is the code that creates the the main CPropertySheet and attaches the pages, after the user has logged in. The screenshot shown is the major window of the application. Everything will be done in this window with a few modal popup windows. I updated my code to also show where I called the modal login dialog. Once the user logs in, I wish to show the propertysheet that contains tabbed pages. Thanks. – Kyle Williamson Mar 13 '15 at 15:35
  • I am also going to try and remove the "Apply" and "Help" buttons. Each CPropertyPage has its own class and makes it easier for me to work on the application. – Kyle Williamson Mar 13 '15 at 15:40
  • This is the link I am using: [Creating Tabbed Dialogs](http://www.technical-recipes.com/2011/creating-a-tabbed-dialog-using-mfc-property-sheets/) – Kyle Williamson Mar 13 '15 at 15:44

1 Answers1

1

Your problem lies in trying to construct a property sheet within a dialog based application. Actually, your choice of executing everything within InitInstance can be problematic at times.

For starters, there's no need to create all of your objects on the heap (ie. using 'new'). But, if that's what you want, ok. As for your original problem of the sheet only displaying for a moment, InitInstance is designed to return immediately if not told otherwise. Thus, you see the sheet for an instance. This is due to MFC expecting a valid pointer to the CWinApp class derived member variable called 'm_pMainWnd' (actually, CWinThread::m_pMainWnd). If you want to start a property sheet, or, main dialog from within InitInstance, you need to set that variable to a valid window. Here's a quick sample I wrote:

CPropertySheet* m_pdlgPropertySheet = new CPropertySheet(_T("Simple PropertySheet"));
    ASSERT(m_pdlgPropertySheet);

    // Add three pages to the CPropertySheet object.  Both m_pstylePage,  
    // m_pcolorPage, and m_pshapePage are data members of type  
    // CPropertyPage-derived classes in CView-derived class.
    Page1* m_pstylePage = new Page1;
    m_pstylePage->Construct(IDD_DIALOG1);
    Page2* m_pcolorPage = new Page2;
    m_pcolorPage->Construct(IDD_DIALOG2);
    m_pdlgPropertySheet->AddPage(m_pstylePage);
    m_pdlgPropertySheet->AddPage(m_pcolorPage);

    m_pMainWnd = m_pdlgPropertySheet;
    INT_PTR nResponse = m_pdlgPropertySheet->DoModal();

Note the line above DoModal. If you need additional info, take a look at Creating a full application using the CPropertySheet. Lastly, you may want to read up on how MFC starts an application and what is expected.

rrirower
  • 4,338
  • 4
  • 27
  • 45
  • Thank you for your answer. I was definenlty making a mistake there. I made the changes that you said with setting `m_pMainWnd` to my property sheet and using `INT_PRT nResponse =`. The window still opens and then closes though. On further research it may be that I need at least one modal window. I am trying to now find how to set a window to modal instead of modeless. – Kyle Williamson Mar 13 '15 at 17:34
  • Setting the property "System Modal" to true for each page did not work. – Kyle Williamson Mar 13 '15 at 17:38
  • I have found one solution, but it does not make sense to me. At the end of InitInstance(), I did `return TRUE`. This keeps my window open when I create the window with `Create()` instead of `DoModal()`. – Kyle Williamson Mar 13 '15 at 17:50
  • 1
    I would recommend you use the sample I posted, or, the article link as a starting point for your code. I think you're adding more code than you really need to. – rrirower Mar 13 '15 at 17:51
  • I used your example exactly, but changed it to fit my variables. It did however not help. I found [this question](http://stackoverflow.com/questions/8853756/mfc-modeless-dialog-close-immediately) on stack overflow that was somewhat similar. I beleive the problem occurs because all the CPropertyPages of my CPropertySheet are considered to be modeless which return 0 right away to nResponse. If you could, take a look at the accepted answer on that questions and let me know what you think. I wish I could upvote you more! – Kyle Williamson Mar 13 '15 at 17:59
  • 1
    While the accepted answer will work, I think it boils down to why do you need a mode less property sheet? If that's not a requirement, you should use modal processing. A better understanding of the [Life Cycle of a Dialog Box](https://msdn.microsoft.com/en-us/library/175y2765.aspx) may help you decide which you really need. – rrirower Mar 13 '15 at 18:06