Open Form2 from Form1, close Form1 from Form2

后端 未结 8 703
陌清茗
陌清茗 2020-12-03 13:06

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

Question: in the form2, I want to create a button

8条回答
  •  心在旅途
    2020-12-03 13:44

    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();
        }
    }
    

提交回复
热议问题