• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2384
  • Last Modified:

Close parent and child Form C#

Hi, I'm currently handling with 3 Forms.
A Main Form,Parent Form and a child Form.
The relation is the Main form has a link to the parent form.
The Parent Form links to a Child Form after a button event.

My question is How can Close the ChildForm and ParentForm at the same time when I close the ChildForm?
//Main Form link
            parentForm Pdetails = new  parentForm();
            Pdetails.ShowDialog();
            Pdetails.Dispose();
 
//Parent Form
                childForm Cconfirm = new childForm(confirmlist);
                Cconfirm.Show();
 
//Child Form (How to close ParentForm also???)
            this.DialogResult = DialogResult.Cancel;
            this.Close();

Open in new window

0
csharp_learner
Asked:
csharp_learner
  • 2
1 Solution
 
anarki_jimbelCommented:
OK, my scenario and some code:

I have a parent form, child form and grandchildfor. (I use these names, not your names because I had a test projects with these names).

In the main (parent) form I open child form:

--->  see snippet 1

From the child form I open grandchild form. However, what's important - grandchild form has special constructor so that it has a pointer to it's "parent" (child form). We will use this reference to close the parent.

---> see snippet 2 (grandchild constructor and code to show grandchild

Now when you show grandchild - try to close it. And write the code like in the snippet 3.
What's doing: when grandchild is closing - it calls to close it's parent.

The trick will not work if you will try to set grandChild.Owher = childForm

and try to call this.Owner.Close(); from the grandchild. The attempt will cause stachoverflow. This is because when you try to close the owner - it tries to close you, and agaia and again...
==snippet 1
        private MyChildForm child;
 
        // button to show a child
        private void button1_Click(object sender, EventArgs e)
        {
 
                // create new child constructor
                child = new MyChildForm();
                child.ShowDialog();
            }
        }
 
======= snippet 2 :
 ... garndchild form constructor:
    public partial class GrandChildForm : Form
    {
        // private variable that points to a "parent"
        private Form thisFormParent;
 
        // constructor
        public GrandChildForm(Form someParentForm)
        {
            InitializeComponent();
            thisFormParent = someParentForm;
        }
 
... in child form - call to create and show grandchild
        private void button1_Click(object sender, EventArgs e)
        {
            GrandChildForm grahdchild = new GrandChildForm(this);
            grahdchild.Show();
        }
 
 
===== snippet 3 - closing event in grandchild:
 
    public partial class GrandChildForm : Form
    {
        // private variable that points to a "parent"
        private Form thisFormParent;
 
        // constructor
        public GrandChildForm(Form someParentForm)
        {
            InitializeComponent();
            thisFormParent = someParentForm;
        }
 
        private void GrandChildForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            thisFormParent.Close();
        }
 
    }

Open in new window

0
 
csharp_learnerAuthor Commented:
Thanks for the reply,I followed your step but my parent Form
        private void newDataPoint_Click(object sender, EventArgs e)
        {
            MyChildForm child = new MyChildForm();
            child.ShowDialog();
            child.Dispose();
        }
"MyChildForm child = new MyChildForm();"does not take '0' arguments.What should I include inside?
I tried MyChildForm child = new MyChildForm(this); but doesn't work
0
 
csharp_learnerAuthor Commented:
my bad MyChildForm child = new MyChildForm(this); worked.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now