943,923 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2533
  • C++ RSS
May 12th, 2008
0

this->form2instance.Show();

Expand Post »
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.


C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
May 12th, 2008
0

Re: 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.
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
May 12th, 2008
0

Re: this->form2instance.Show();

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

C++ Syntax (Toggle Plain Text)
  1. Form2 newForm;
  2. Application->CreateForm( Form2, newForm);
  3. newForm.Show();



Click to Expand / Collapse  Quote originally posted by Duoas ...
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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
May 12th, 2008
0

Re: this->form2instance.Show();

Below is one option, maybe that is what you are after
C++ Syntax (Toggle Plain Text)
  1. private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e)
  2. {
  3. Form2 ^form2 = gcnew Form2;
  4. form2->Show();
  5. }
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
May 12th, 2008
0

Re: this->form2instance.Show();

Alas, I'm used to doing this in Delphi. Sorry for the syntax problems in C++.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
May 12th, 2008
0

Re: this->form2instance.Show();

This works great.

Click to Expand / Collapse  Quote originally posted by mitrmkar ...
Below is one option, maybe that is what you are after
C++ Syntax (Toggle Plain Text)
  1. private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e)
  2. {
  3. Form2 ^form2 = gcnew Form2;
  4. form2->Show();
  5. }
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: ScrollBar on Form
Next Thread in C++ Forum Timeline: Please help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC