I have this code that opens a Form. This code does work but if I just close the Form that did open with this code and press the button5 again, I will have an Errormessage:

"Cannot access a disposed object. Object name: ´Form2´."
But if I change:
this->form2instance.Show();

to:

this->form2instance.ShowDialog();

Then it does work, I can open the Form2 and close it, open it again and so on.
The thing is that I need to use the code below because I will have to open many instances of the same Form.

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

       this->form2instance.Show();
		 
}

Typically all the forms you create in the IDE are automatically created when your application starts. (You can choose which forms are auto-created from the Project->Options menu.) You can see the code that does it by choosing Project->View Source (to see the WinMain() function).

What appears to be happening is that you are Free()ing the form when it is closed, and you can no longer Show() an object that has been destroyed.

From what I understand that you want to do, you should go to the project options and remove the Form2 from the auto-create list. (You can also delete the Form2 variable from the unit source file.)
Next, in your button click handler, (1) create a new instance of the form and (2) show it.

System::Void Form1::button5_Click(System::Object^  sender, System::EventArgs^  e)
{
  TForm2 newForm;
  Application->CreateForm( TForm2, newForm );  // (1)
  newForm.Show();  // (2)
}

Now you can click the button to get as many copies of TForm2 as you want, and closing each one should still delete the individual instance.

Hope this helps.

I have deleted the Form2 variable from the unit source file.
Then I have written this in the button5_Click event handler.

If I write TForm2 newForm as described before, this will be an undeclared identifier.
But Form2 newForm; compiles.
I cant find ->CreateForm as a member to Application.
I have to write Application:: to find any members at all but here, I cant find "CreateForm".
I am not sure if I am doing right or if I could do something else.
Thanks...

Form2 newForm;
Application->CreateForm( Form2, newForm);
newForm.Show();

Typically all the forms you create in the IDE are automatically created when your application starts. (You can choose which forms are auto-created from the Project->Options menu.) You can see the code that does it by choosing Project->View Source (to see the WinMain() function).

What appears to be happening is that you are Free()ing the form when it is closed, and you can no longer Show() an object that has been destroyed.

From what I understand that you want to do, you should go to the project options and remove the Form2 from the auto-create list. (You can also delete the Form2 variable from the unit source file.)
Next, in your button click handler, (1) create a new instance of the form and (2) show it.

System::Void Form1::button5_Click(System::Object^  sender, System::EventArgs^  e)
{
  TForm2 newForm;
  Application->CreateForm( TForm2, newForm );  // (1)
  newForm.Show();  // (2)
}

Now you can click the button to get as many copies of TForm2 as you want, and closing each one should still delete the individual instance.

Hope this helps.

Below is one option, maybe that is what you are after

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

Alas, I'm used to doing this in Delphi. Sorry for the syntax problems in C++.

This works great.

Below is one option, maybe that is what you are after

private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) 
{
    Form2 ^form2 = gcnew Form2;
    form2->Show();
}
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.