How to close parent form from child form in Windows Forms 2.0?

I need to close the parent form from a child form from a Windows application. What would be the best way to do this?

+3
source share
7 answers

When you close a form in WinForms, it distributes all its children. So this is not a good idea. You must do this asynchronously, for example, you can send a message to the parent form.

+3
source

I looked at this blog post, which looks like it will work, and uses the concept of the event handler from the D2VIANT answer

http://www.dotnetcurry.com/ShowArticle.aspx?ID=125

: 1. Windows. Visual Studio 2005 2008. "" > "" > "" > " Visual Basic Visual # " " > " Windows ". > .

2: n ew . > > Windows Forms > Form2.cs > .

3: Form1 "btnOpenForm" , . . frm2_FormClosed, :

    private void btnOpenForm_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
        frm2.Show();
        this.Hide();
    }


    private void frm2_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close();
    }
+4

Form "" , ( MDI, MDIParent). , , , .

0

, , , , . ( ).

0

. - MVC MVP, .

MVP MVC.

0

; , :

private Form pForm;
public ChildForm(ref Form parentForm)
{
    pForm = parentForm;
}

private closeParent()
{
    if (this.pForm != null)
        this.pForm.Close();
    this.pForm = null;
}
0

.

(): ( ) > . VIA > ( ), .

:

: Process.Start("Your_App's_EXE_Full_Path.exe");

: , :

  • string FullPath = Environment.CurrentDirectory + "\\YourAppName.exe";

  • Process.Start(FullPath);.

  • this.Close();

    • , .
0
source

Source: https://habr.com/ru/post/1696650/


All Articles