locked
New dialog NOT displayed RRS feed

  • Question

  • Hi,

    I added a new button in existing dialog and created a new dialog 'IDD_SETTINGS" with MFC which contains a couple of edit boxes and OK button. Before adding the edit boxes, I could open the new dialog from the existing one. When I added edit boxes and member variables, suddenly my dialog box is not showing up at all.

    The DoModal returns -1. what could be the reason??

    Please help me to resolve this error. Many thanks!


    • Edited by huskyfinch Wednesday, June 8, 2016 8:35 AM
    Wednesday, June 8, 2016 5:49 AM

Answers

  • Could you please tell me how to check this? There is no initDialog method in the code.

    I'm not talking about "initDialog", whatever that is.  I'm talking about the dialog class constructor.  See the following small dialog class.  The constructor specifies the dialog's resource identifier.  That MUST be the same as the identifier used in the resource editor.  For example, the following specifies IDD_SETTINGS in the constructor.  If the actual resource identifier (as seen in the resource editor) is some other value, like IDD_BOGUS, DoModal will fail.

    // Test2.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "MFCApplication1.h"
    #include "Test2.h"
    #include "afxdialogex.h"
    
    
    // CTest2 dialog
    
    IMPLEMENT_DYNAMIC(CTest2, CDialog)
    
    CTest2::CTest2(CWnd* pParent /*=NULL*/)
    	: CDialog(IDD_SETTINGS, pParent)
    {
    
    	m_str = _T("");
    	m_int = 0;
    }
    
    CTest2::~CTest2()
    {
    }
    
    void CTest2::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	DDX_Control(pDX, IDC_EDIT1, m_E1);
    	DDX_Control(pDX, IDC_EDIT2, m_E2);
    	DDX_Control(pDX, IDC_EDIT3, m_E3);
    	DDX_Text(pDX, IDC_EDIT1, m_str);
    	DDX_Text(pDX, IDC_EDIT2, m_int);
    }
    
    
    BEGIN_MESSAGE_MAP(CTest2, CDialog)
    END_MESSAGE_MAP()
    
    
    

    • Proposed as answer by Baron Bi Tuesday, June 28, 2016 1:51 AM
    • Marked as answer by Baron Bi Tuesday, June 28, 2016 5:42 AM
    Wednesday, June 8, 2016 1:10 PM
  • Unfortunately none of the solutions mentioned above worked :(

    There was an older code - before I created and deleted this dialog box. Checked out that code, copied all cpp and h files from new code to the new one and started building the project. Some other dialog box in the code had some newer radio buttons and edit boxes which I had to add manually.

    After that, creation of new Settings dialog box actually displayed it!! Finally!!

    • Marked as answer by huskyfinch Wednesday, June 29, 2016 12:33 AM
    Wednesday, June 29, 2016 12:33 AM

All replies

  • I think that the definition of IDD_SETTINGS can be found in Resource.h file.

    What kind of controls have you added? The dialog is not shown if you add RichEdit textboxes, but do not initially call AfxInitRichEdit2.

    Wednesday, June 8, 2016 7:44 AM
  • Yes, resource.h contains this variable. Also resource.h is included from my file. I just added some static text and the edit boxes (for CString and int) and a check box for bool value. I wanted to disable the OK button till check box is clicked so I added code 

    okButton = (CButton *)GetDlgItem(IDOK) ;
    okButton->EnableWindow(FALSE);

    and then later in the code enabled the button using,

    okButton->EnableWindow(TRUE);

    but it stopped showing my new dialog!! So I removed the dialog box and the class and recreated them again. The newly created dialog is not displayed after I added edit boxes :(

    I have never called AfxInitRichEdit. I just added the text boxes! 

    Wednesday, June 8, 2016 7:53 AM
  • Oh The function DoModal returns -1 for my new dialog. 
    Wednesday, June 8, 2016 8:02 AM

  • I have never called AfxInitRichEdit. I just added the text boxes! 

    But are they RichEdit boxes? If so, you must call AfxInitRichEdit().

    David Wilkinson | Visual C++ MVP

    Wednesday, June 8, 2016 9:59 AM
  • Hi David,

    Thanks for your reply. I have confirmed that there are no rich edit boxes, they are the normal edit boxes. DoModal returns -1 and I don't know why it can happen. Can you tell what causes DoModal to fail?

    Wednesday, June 8, 2016 11:50 AM
  • Another potential cause of failure is if the resource identifier of the dialog is not properly specified in the constructor for the related class that is derived from CDialog or CDialogEx.

    • Edited by RLWA32 Wednesday, June 8, 2016 12:26 PM
    Wednesday, June 8, 2016 12:24 PM
  • Could you please tell me how to check this? There is no initDialog method in the code.
    Wednesday, June 8, 2016 1:04 PM
  • I think there is something wrong (no , I know there is something wrong) with the IDE editor of VS2015. You may be seeing the same problem, the IDE vs Program resources do not always agree....

    http://www.ransen.com Cad and Graphics software

    Wednesday, June 8, 2016 1:04 PM
  • Could you please tell me how to check this? There is no initDialog method in the code.

    I'm not talking about "initDialog", whatever that is.  I'm talking about the dialog class constructor.  See the following small dialog class.  The constructor specifies the dialog's resource identifier.  That MUST be the same as the identifier used in the resource editor.  For example, the following specifies IDD_SETTINGS in the constructor.  If the actual resource identifier (as seen in the resource editor) is some other value, like IDD_BOGUS, DoModal will fail.

    // Test2.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "MFCApplication1.h"
    #include "Test2.h"
    #include "afxdialogex.h"
    
    
    // CTest2 dialog
    
    IMPLEMENT_DYNAMIC(CTest2, CDialog)
    
    CTest2::CTest2(CWnd* pParent /*=NULL*/)
    	: CDialog(IDD_SETTINGS, pParent)
    {
    
    	m_str = _T("");
    	m_int = 0;
    }
    
    CTest2::~CTest2()
    {
    }
    
    void CTest2::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	DDX_Control(pDX, IDC_EDIT1, m_E1);
    	DDX_Control(pDX, IDC_EDIT2, m_E2);
    	DDX_Control(pDX, IDC_EDIT3, m_E3);
    	DDX_Text(pDX, IDC_EDIT1, m_str);
    	DDX_Text(pDX, IDC_EDIT2, m_int);
    }
    
    
    BEGIN_MESSAGE_MAP(CTest2, CDialog)
    END_MESSAGE_MAP()
    
    
    

    • Proposed as answer by Baron Bi Tuesday, June 28, 2016 1:51 AM
    • Marked as answer by Baron Bi Tuesday, June 28, 2016 5:42 AM
    Wednesday, June 8, 2016 1:10 PM
  • Try Calling InitCommonControls() before your DoModal() and see if it helped.

    Thanks


    Rupesh Shukla

    Wednesday, June 8, 2016 4:00 PM
  • Unfortunately none of the solutions mentioned above worked :(

    There was an older code - before I created and deleted this dialog box. Checked out that code, copied all cpp and h files from new code to the new one and started building the project. Some other dialog box in the code had some newer radio buttons and edit boxes which I had to add manually.

    After that, creation of new Settings dialog box actually displayed it!! Finally!!

    • Marked as answer by huskyfinch Wednesday, June 29, 2016 12:33 AM
    Wednesday, June 29, 2016 12:33 AM