returning (back) the windows form c++

I want to create multiple forms and when I click the back button it will back to form1. I tried this C++/CLI - how to open a new form and back but when I click the button in the form2 to go back to form1 it gets an error. " NullReferenceException was Unhandled"

Form1

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

            /* Form1::Hide();
             Form2^ form2 = gcnew Form2();
             form2->ShowDialog();*/


             Form2 ^ frm2 = gcnew Form2();
             frm2->Show();
             this->Hide();
         }

Form2

Form2(System::Windows::Forms::Form ^ frm1)
{
    otherform = frm1;
    InitializeComponent();

}

private: System::Windows::Forms::Form ^ otherform;

#pragma endregion
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e
 {

 this->Hide();
 otherform->Show();   
 }

Upvotes: 2

Views: 3395

Answers (1)

sgarizvi
sgarizvi

Reputation: 16816

You have created the second constructor of Form2 but you are not using it inside button1_Click.

Instead of this:

Form2 ^ frm2 = gcnew Form2();

Do this:

Form2 ^ frm2 = gcnew Form2(this);

Upvotes: 1

Related Questions