3
votes

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

8
How about putting the question in the subject line instead of stating that you have one.Neil N
Welcome to StackOverflow, hcemp! You'll want to put the question in the subject line next time, similar to the way that editors already have. More guidelines available here: stackoverflow.com/faq and here: meta.stackexchange.com/questions/tagged/faqChristian Mann
sorry,i'm fresh and my English is very poolhcemp

8 Answers

8
votes

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

This works:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Hide()
    Form2.Show()
0
votes

Your question is vague but you could use ShowDialog to display form 2. Then when you close form 2, pass a DialogResult object back to let the user know how the form was closed - if the user clicked the button, then close form 1 as well.

0
votes

on the form2.buttonclick put

this.close();

form1 should have object of form2.

you need to subscribe Closing event of form2.

and in closing method put

this.close();
0
votes

if you just want to close form1 from form2 without closing form2 as well in the process, as the title suggests, then you could pass a reference to form 1 along to form 2 when you create it and use that to close form 1

for example you could add a

public class Form2 : Form
{
    Form2(Form1 parentForm):base()
    {
        this.parentForm = parentForm;
    }

    Form1 parentForm;
    .....
}

field and constructor to Form2

if you want to first close form2 and then form1 as the text of the question suggests, I'd go with Justins answer of returning an appropriate result to form1 on upon closing form2

0
votes

I did this once for my project, to close one application and open another application.

    System.Threading.Thread newThread;
    Form1 frmNewForm = new Form1;

   newThread = new System.Threading.Thread(new System.Threading.ThreadStart(frmNewFormThread));
this.Close();
        newThread.SetApartmentState(System.Threading.ApartmentState.STA);
        newThread.Start();

And add the following Method. Your newThread.Start will call this method.

    public void frmNewFormThread)()
    {

        Application.Run(frmNewForm);

    }
0
votes
//program to form1 to form2
private void button1_Click(object sender, EventArgs e)
{
    //MessageBox.Show("Welcome Admin");
    Form2 frm = new Form2();
    frm.Show();
    this.Hide();          
}
0
votes
private void button1_Click(object sender, EventArgs e)
{
      Form2 m = new Form2();
      m.Show();
      this.Visible = false;
}