paytam
paytam

Reputation: 347

Owned form and mdi parent

this is my scenario and hope you can solve it for me

I have a MDI container form called "MainForm". In MainForm there is a simple form call "Form1". In Form1 there is a button. every time you pushed it, It open a new form which instance of "Form2". The followng code is click button event.

Button_Click()
{
   Form2 frm=new Form2();
   frm.mdiparnt=this.MdiParent;
   this.addOwnedForm(frm);
   frm.Visible=true;
}

and the following code tries to close owned forms when the user close Form1

Form1_CloseEvent()
{
   foreach(var item in this.ownedForm)
   {
      item.close();
   }
}

But when the debugger steps into close event, just close Form1, and the form2 instances remain open. what should I do to solve it

Upvotes: 0

Views: 1100

Answers (2)

No Idea For Name
No Idea For Name

Reputation: 11597

first of all this code does not compile!

you have several syntax errors: mdiparnt, addOwnedForm, ownedForm, close

you are probably not sharing your actual code and that's gonna be a problem to help you if it's not your code.

now in Button_Click() event you are doing

frm.mdiparnt=this.MdiParent;
this.AddOwnedForm(frm);

even though you only need

this.AddOwnedForm(frm);

or an exception will be thrown. i've checked this code and it's working just fine

Upvotes: 1

Shaharyar
Shaharyar

Reputation: 12449

I think you are not setting up the event. Do it like this.

Add it to your Button_Click() method:

this.FormClosed += Form1_FormClosed;

Here is the method:

void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    foreach(var item in this.ownedForm)
    {
        item.close();
    }
}

Upvotes: 1

Related Questions