Link to home
Start Free TrialLog in
Avatar of escheider
escheider

asked on

Opening a form

Good morning experts:

I'm new to the Visual C++.NET world, so I'd appreciate the help with what should be an easy task.  I have created a Windows Forms application in my environment with the main MDI form called frmMain.h   On this form, I have a menu and would like to open a form called frmSchedule.h from one of the menu options.  I've tried to follow a few of the examples on Experts Exchange, but I keep getting errors.  I'd appreciate some help with this task.

Here is the event for the menu item:

private: System::Void scheduleBackupToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
      /// open frmSchedule form here
             }

any help appreciated.
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of _davidharris
_davidharris

I thought was just "form->show();"

frmSchedule->Show();

As far as I know that will work.

There's also frmSchedule->ShowDialog() which will open your form modal-ly (sp?), meaning the form will gain the total focus of your application and the user will only be able to use that form until they close it. It sounds like you don't need that, but just in reference to opening forms in general, you will need to know that.

Remember to include the frmSchedule.h file in your application_name.cpp file under whatever you've change form1.h to.

i.e. :

#include "stdafx.h"
#include "frmMain.h"
#include "frmSchedule.h"        // <-- that line will not appear automatically, and you will not be able to call
                                            // form->show() without it!

using namespace frmMain;     // using namespace "whatever_you've_named_it";

[STAThreadAttribute]
...

Hope that helps! Like I said, I'm not sure if that will open an MDI form!

_david
Avatar of escheider

ASKER

Okay Alex, your solution worked with one minor change, so could you explain some of the syntax so I can understand it ..

What does the following line do:

private: frmSchedule^ frmschedule;

and this line:

if ( frmschedule == nullptr )

This is required if you want to keep only one instance of child form and keep reference to it. If you want to keep multiple instances, you can do this by your way. In any case created forms can be accessed using MdiChildren Property of parent form.
okay Alex .. one last question.  Since I check to see if frmschedule is already referencing an object [in this case a form], how do I destroy that object when I close the frmSchedule form?  

Right now, if I open the frmSchedule.h form using your syntax, then try to reopen it again, frmschedule still references the object and therefore bypasses the frmschedule->show(); line of code.
Subscribe to FormClosing event of frmschedule in the main form and set reference to nullptr in event handler.
Alex:

Please remember that I'm fairly new to C++.NET, so I'm not familar with some of what you are saying.  Do you have an example?
frmschedule = gcnew frmSchedule();
frmschedule->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &MainForm::Form1_FormClosing);
...

private: System::Void frmschedule_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
    frmschedule = null;
             }