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.
LVL 4
escheiderAsked:
Who is Participating?
 
AlexFMCommented:
private frmSchedule^ frmschedule;

private: System::Void scheduleBackupToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
{
    if ( frmschedule == nullptr )
    {
        frmschedule = gcnew frmSchedule();
        frmschedule->MdiParent = this;
        frmschedule->Show();
    }
}
0
 
_davidharrisCommented:
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
0
 
escheiderAuthor Commented:
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 )

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
AlexFMCommented:
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.
0
 
escheiderAuthor Commented:
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.
0
 
AlexFMCommented:
Subscribe to FormClosing event of frmschedule in the main form and set reference to nullptr in event handler.
0
 
escheiderAuthor Commented:
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?
0
 
AlexFMCommented:
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;
             }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.