从Form1中打开Form2,从Form2中关闭Form1 [英] Open Form2 from Form1, close Form1 from Form2

查看:107
本文介绍了从Form1中打开Form2,从Form2中关闭Form1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我有两种形式,分别称为form1和form2,在form1中有一个按钮,当我单击它时,然后打开form2

Now, I have two forms, called form1 and form2, in the form1 there's a button, when I click it, then open the form2

问题:在在单击form2时,我想创建一个按钮,关闭form2并关闭form1。

Question: in the form2, I want to create a button when I click it, the form2 close and the form1 close. How to do?this

推荐答案

Form1:

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2(this);
        frm.Show();
    }

Form2:

public partial class Form2 : Form
{
    Form opener;

    public Form2(Form parentForm)
    {
        InitializeComponent();
        opener = parentForm;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        opener.Close();
        this.Close();
    }
}

这篇关于从Form1中打开Form2,从Form2中关闭Form1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆