Hello. I've got a problem that I haven't been able to solve after a long time searching.

It's a simple problem. I have two forms.
1) Form1
2) Form2

I need to get from Form1 to Form2 and vice versa.

I can get from Form1 to Form2 easily, but I can't get back from Form2 to Form1.

I have the #include "Form2.h" on Form1.h and the following code in the click event of button1.

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				
				 Form1::Visible=false;
				 Form2 ^form2 = gcnew Form2();
				 form2->ShowDialog();
			 }

Does anyone know how to solve this? Thanks.

I have a project that does something similar and it works ok, except uses Show(), not ShowDialog(). But I changed it to use ShowDialog() and it worked too.

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

             Form2^ f2 = gcnew Form2;
             f2->Show();

         }
};

>>but I can't get back from Form2 to Form1.

You don't have to do anything special, when Form2 is closed Form1 automatically regains control as if nothing had happened.

Yes, at the moment, that's all I can do as well. However it's not really practical because if form1 is not visible, then once you close form2, form1 won't show.

Also, if you move form2 around, you can see form1 behind it.

If you want to hide Form1 while Form2 is up then just set its Visible property to false

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

             Form2^ f2 = gcnew Form2;
             this->Visible = false;
             f2->ShowDialog();
             this->Visible = true;

         }
};

Wow...It was THAT simple. I spent so long trying to figure this out and it was that simple lol.

So once the ShowDialog() is called, anything after it is not run until the object of ShowDialog is closed right?

Thanks alot for the answer.

>>So once the ShowDialog() is called, anything after it is not run until the object of ShowDialog is closed right?


Yes -- but not the same with Show().

ok, thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.