?
Solved

Opening a form

Posted on 2006-05-03
8
Medium Priority
?
238 Views
Last Modified: 2010-04-24
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.
0
Comment
Question by:escheider
  • 4
  • 3
8 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 16597251
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
 
LVL 1

Expert Comment

by:_davidharris
ID: 16597344
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
 
LVL 4

Author Comment

by:escheider
ID: 16597512
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 48

Expert Comment

by:AlexFM
ID: 16597838
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
 
LVL 4

Author Comment

by:escheider
ID: 16598591
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 16598963
Subscribe to FormClosing event of frmschedule in the main form and set reference to nullptr in event handler.
0
 
LVL 4

Author Comment

by:escheider
ID: 16599035
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 16603004
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question