So I need to be able to close a window when I open another window... Here is my code:

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

All this does is close both windows... If you guys could help out a noob to C++ that would be greatly appreciated... Thank you!

Whether you use Show() or ShowDialog() depends on whether you want a modeless or model dialog window. Below code works for model dialogs. First close Form1 then show Form2. After Form2 is closed you have to show Form1 to make it visible again.

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

Edited 6 Years Ago by Ancient Dragon: n/a

I'm really sorry... But I made a mistake. This is the actual problem code. The code I posted above wouldn't compile. This is the real code I have a problem with....

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
				 Form2 ^form2 = gcnew Form2();
				 form2->Show();
				 this->Close();	
		 }

See if using this->Hide() on line 5 gives you the effect that you want. Your first dialog will still be in memory. Unless your dialog has lots of controls on it, this shouldn't be a problem.

I think this has something to do with the fact that the second dialog is an object in the first dialog, so killing the first dialog (I think, it's logical) calls first's destructor which gets rid of the second dialog. I was getting the behavior that they both stay open, so there may be measures against this that are happening behind the scenes.

If this does not answer your question, see what AD can come up with, and if it still doesn't work try it over in C# (specify that you have asked it in C++ already). A slightly different syntax would be involved, but the same overall method that someone suggests should work.

This question has already been answered. Start a new discussion instead.