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.

Recommended Answers

If you want to hide Form1 while Form2 is up then just set its Visible property to false
[code]
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

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

     }

};
[/code]

Jump to Post

All 6 Replies

Re: VC++ switch between windows forms 80 80

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.

Re: VC++ switch between windows forms 80 80

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.

Re: VC++ switch between windows forms 80 80

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;

         }
};
Re: VC++ switch between windows forms 80 80

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.

Re: VC++ switch between windows forms 80 80

>>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().

Re: VC++ switch between windows forms 80 80

ok, thanks again.

Be a part of the DaniWeb community

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