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.
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
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.
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
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.
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
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.
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.
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?
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.
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:
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.
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.