# [SOLVED] [C# Net] Modal Forms



## Emphyrio (May 16, 2009)

Hi all 

I searched the Net for this "common" problem, but couldn't find a proper solution.
Let me start to descripe the problem I'm facing....

Objective:


Open a second Windows Modal form from within first form by means of a button.
First form stays in controle (hence Modal).
If I close the second form by clicking the red X control,
it has to close and first form must be still on his place on the screen.
When I click on button, second form has to reappear, and so on....
The procedure for this is :



> namespace TestModalForms
> {
> public partial class Form1 : Form
> {
> ...


So far so good, but there is a catch in this......
Everytime I click on the button (on Form1), there will be a new instance of a Form2. And I don't want that.

So, the solution would be to place the "Form2 secondForm = new Form2();" out of the button click methode like so:



> namespace TestModalForms
> {
> public partial class Form1 : Form
> {
> ...


Now when I click the button on Form1 to open Form2, it does show Form2.
If I click it again, it doesn't open a second Form2.
When I close Form2, it closes and Form1 stays.










So far so good, but (yes there is a but ), when I click on the button of Form1 to reopen Form2 I get a error:



> System.ObjectDisposedException was unhandled
> Message=Geen toegang tot een verwijderd object. ( <= Translated: No access to a deleted object)
> Objectnaam: Form2.
> Source=System.Windows.Forms
> ObjectName=Form2


If I catch the exception, I'm stuck with the first problem again: several open Form2's.

Any idea's how to solve this ?


----------



## niemiro (Feb 20, 2010)

*Re: [C# Net] Modal Forms*

Hello!

If I understand correctly, the code you have in Form1 is correct (taking the *Form2 secondForm = new Form2();* ) code outside of the button1_Click event, but it is Form 2 which needs some work. 

Basically, you don't want to delete Form2 every time your close it. To achive this, handle the Form2: Form_Closing event, and add this code:


```
this.Hide();
e.Cancel = true;
```
Does that solve the problem or not?

Thanks a lot, and the very best of luck!

Richard


----------



## Emphyrio (May 16, 2009)

*Re: [C# Net] Modal Forms*

Hi niemiro,

First off all, thank you for the swift reply 
And yes, that was the solution 

In Form2, I added the following code:


> namespace TestModalForms
> {
> public partial class Form2 : Form
> {
> ...


And it works as a charm  

Thank you very much :thumb:


----------



## Emphyrio (May 16, 2009)

* Update *

I added a Exit button in Form 1, whitch leads to a Application.Exit()
Now this Exit button won't work, the red x works.




> public Form1()
> {
> InitializeComponent();
> }
> ...





I 'm missing something here....


----------



## Emphyrio (May 16, 2009)

*Re: [C# Net] Modal Forms*

_Sorry for the post, but I can't edit any more after 15 minutes..._

I found the solution in adding a dispose property.




> public partial class Form1 : Form
> {
> public Form1()
> {
> ...


All well and many thanks again :thumb:


----------



## niemiro (Feb 20, 2010)

Hello!

I am glad that your problem is now resolved. Sorry about not mentioning the exiting problem. I really should have...

If you, in some real application rather than just this demonstration, have multiple forms which you want to close without adding an extra line here each time, you can use this code:


```
foreach (Form form in this.MdiChildren)
{
    form.Dispose();
}
```
However, be very careful. Note that this disposes all forms. None are just closed. Therefore, Form_Closing and Form_Closed events will not fire for any form.

Richard


----------



## Emphyrio (May 16, 2009)

Thanks again Richard :thumb:

I was able to use the "demo model" discribed here and implement it in my application "*PC Info Helper Tool*" I made for using PC Info.
I use it to show a pic in a modal windows form, so the Helper has a reference.

It works as expected. :smile:


----------

