Hey all,

I am having some trouble trying to open a dialog window. Firstly I am using MFC in MSVC 6. I have a main dialog window and I am trying to open a second dialog window by clicking on a button("second") in the main dialog window. When I am in the main window I am using

if(Dlg.DoModal() == IDC_SECOND)
{
	CSecondDialog dlg;
	dlg.DoModal();
}

to try and initate the second window. I am getting now compile time errors but when I click on the "second" button nothing happens. Does anyone have any ideas??? Thanks in advance.

Colm

Open your main dialog in Resource Editor. Double click the button and a button handler is created in your main dialog .cpp file. Place your two lines of code

CSecondDialog dlg;
dlg.DoModal();

in this button handler.

Hey Jencas,

thanks for the reply. I am double clicking on the "second" button in the main dialog and a "Push Button Properties" window appears. I then click on the FileView button to access my dialog.cpp and the "Push Button Properties" window disappears. I cant find the button handler that you mentioned. Have you any ideas where I am going wrong. Thanks for your help.

Colm

Below are three pictures -- The first shows the basic main dialog with one "second" button. The second shows the window after double clicking on it, and the third shows what happens after hitting the Ok button

Attachments dlg1.jpg 119.83 KB dlg2.jpg 126.17 KB dlg3.jpg 106.69 KB

Hey AncientDragon,

thanks for the great visual reply. I tried your method in MSVC 2008 and it works. Unfortunately I have all of my code in MSVC 6.0 so I get compile errors if I use MSVC 2008. Do you know how to implement your method for MSVC 6.0, if I double click on button I get a "Push Button Properties" window, is there a way to get a "add member function" window in MSVC 6.0??? Thanks for your help.

Colm

with VC6:
1. double click on the button in the ressource editor and provide a name for the button handler in the "add member function" window which appears or
2. right click on the button in the ressource editor, select class wizard, and then select BN_CLICKED message. Press the "add function" button to the right of the window.

You need to create the dialog class source code with the class wizard before you can create the button handler!!

All text in "" are translated from the german version of VC6, so they may vary in your version

Hey AncientDragon,

thanks for the great visual reply. I tried your method in MSVC 2008 and it works. Unfortunately I have all of my code in MSVC 6.0 so I get compile errors if I use MSVC 2008. Do you know how to implement your method for MSVC 6.0, if I double click on button I get a "Push Button Properties" window, is there a way to get a "add member function" window in MSVC 6.0??? Thanks for your help.

Colm

That was vc6 that I posted. MFC for VC6 and VC2005/8 are not compatible -- Microsoft made a lot of changes to MFC including replacing many classes with templates.

Hey Jencas,

sorry about this late reply, I was away :). Thanks for your second reply, it made things alot easier (I should have pointed out that I am far from at home with C++). I have my program running without errors with your method

CSecondDialog dlg;
dlg.DoModal();

in the button handler. When I run the program and click the button nothing happens though. Would you have any idea whats happeing here, thanks again for all your help, cant believe something that seems as simple as opening a dialog box from a button can be this complex?? I'm hoping its one of those, do it once and its easy jobs :). Thanks again.

Colm

Have a look at the MESSAGE_MAP of your main dialog (that 's what is between the BEGIN_MESSAGE_MAP and the END_MESSAGE_MAP lines). Is there a line

ON_BN_CLICKED(

followed by the IDC_... of your button? If yes, what follows the IDC? Is what follows the name of the method which contains the two lines for opening the second dialog?

Hey Jencas,

thanks for reply. The message map entry I put in myself, it looks like:

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_COMMAND (ID_SHOWDIALOG, ShowDialog)
ON_BN_CLICKED(IDC_SIM, OnSim)
END_MESSAGE_MAP()

IDC_SIM is the ID of the button and OnSim is the name of the function. In my main dialog I am trying to initiate the button by putting in:

if(Dlg.DoModal() == IDC_SIM)
{
CSimConFig dlg;
dlg.DoModal();
}

with CSimConFig being the name of the class for the second dialog box. I am getting no compile errors but nothing happens when I click on the IDC_SIM button. Have you any ideas, thanks again.

Colm.

When you double click on the button as I showed previously the VC++ 6.0 IDE will create the correct message map entry for you -- you do NOT have to code that yourself. After clicking that button, the message map in the *.cpp file should like something like this:

BEGIN_MESSAGE_MAP(CDlgtestDlg, CDialog)
	//{{AFX_MSG_MAP(CDlgtestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

And in the *.h file

protected:
	HICON m_hIcon;

	// Generated message map functions
	//{{AFX_MSG(CDlgtestDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnButton1();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
}

Attached is the complete project I made, minus compiler-generated files.

Your OnSim should look like:

void YourFirstDialog::OnSim()
{
  CSimConFig dlg;
  dlg.DoModal();
}

Please reread the manual concerning the return values of DoModal(). There is no point in comparing the return value against control IDs.
Another strange thing: are you sure the Button is in CMainFrame? Usually you only have IDs of menu items with ON_COMMAND handlers there.

Comments
Thanks for all the great help, much appreciated

>>Are you sure the Button is in CMainFrame?

Dialog projects do not have a CMainFrame class (assuming he created a dialog project, not SDI or MDI)

Hey AncientDragon and Jencas,

thanks a million for all your help. From looking at your example AncientDragon I think I may finally understand what is going on. Again thank you for posting that. Could I ask one small favour from you guys, could you recommand a good book for MFC, I have "Introduction to MFC Programming with Visual C++" by Richard M. Jones and "Professional MFC with Visual C++ 6" by Mike Blaszczak but im finding it hard to dig what I want from these two books. Is there any "bible" book for MFC like "Programming principles and practice using C++" by Bjarne Stroustrup book is for C++? Thanks again for all your help and time.

Colm.

There have been many major changes to MFC since that compiler was released. I started learning it by doing the Scribble Tutorial

That tutorial will not be useful on VC++ 2005/8 compilers.

Comments
Thanks for all the great help, much appreciated
This question has already been answered. Start a new discussion instead.