Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Form2 from Form1, close Form1 from Form2

Tags:

c#

winforms

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 when I click it, the form2 close and the form1 close. How to do?this

like image 848
hcemp Avatar asked Oct 22 '25 11:10

hcemp


1 Answers

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();
    }
}
like image 178
SiN Avatar answered Oct 24 '25 02:10

SiN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!