this->form2instance.Show();

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

this->form2instance.Show();

 
0
  #1
May 12th, 2008
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.


  1. private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e)
  2. {
  3.  
  4. this->form2instance.Show();
  5.  
  6. }
Last edited by Jennifer84; May 12th, 2008 at 10:23 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: this->form2instance.Show();

 
0
  #2
May 12th, 2008
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.
  1. System::Void Form1::button5_Click(System::Object^ sender, System::EventArgs^ e)
  2. {
  3. TForm2 newForm;
  4. Application->CreateForm( TForm2, newForm ); // (1)
  5. newForm.Show(); // (2)
  6. }
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.
Last edited by Duoas; May 12th, 2008 at 2:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: this->form2instance.Show();

 
0
  #3
May 12th, 2008
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...

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



Originally Posted by Duoas View Post
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.
  1. System::Void Form1::button5_Click(System::Object^ sender, System::EventArgs^ e)
  2. {
  3. TForm2 newForm;
  4. Application->CreateForm( TForm2, newForm ); // (1)
  5. newForm.Show(); // (2)
  6. }
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: this->form2instance.Show();

 
0
  #4
May 12th, 2008
Below is one option, maybe that is what you are after
  1. private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e)
  2. {
  3. Form2 ^form2 = gcnew Form2;
  4. form2->Show();
  5. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: this->form2instance.Show();

 
0
  #5
May 12th, 2008
Alas, I'm used to doing this in Delphi. Sorry for the syntax problems in C++.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: this->form2instance.Show();

 
0
  #6
May 12th, 2008
This works great.

Originally Posted by mitrmkar View Post
Below is one option, maybe that is what you are after
  1. private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e)
  2. {
  3. Form2 ^form2 = gcnew Form2;
  4. form2->Show();
  5. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC