NateShoffner
NateShoffner

Reputation: 16829

C# - Close a child form from parent

I have a parent form and a child form. I need to open the child form at the beginning of a method, do some pretty intensive tasks and then close the child form upon completion.

Here is basically what I've tried so far (with no luck):

Parent Form:

Child child = new Child();

Method()
{
    child.ShowDialog();

    //Method code here

    child.CloseScan();
}

Child Form:

public void CloseScan()
{
    this.Close();
}

Upvotes: 1

Views: 5311

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564323

When you call child.ShowDialog(), the code will halt at that point until the dialog is closed, since you're telling it to function as a modal dialog.

If you want to have the code continue to run, you need to use child.Show(this); instead. You can then do your "method code" and close the window afterwards. (Adding "this" causes the form to be a child form of the current form...)

Upvotes: 2

Related Questions