Close Form2 and show Form1

16,853

Solution 1

Both of the questions you asked can easily be solved with events. Here is the code:

Form1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Declare your new form
        Form2 form2 = new Form2();

        //Register the update event
        form2.updateEvent += new EventHandler(handleUpdateEvent);

        //Register form closed event
        form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);

        Visible = false;

        //Show your new form
        form2.Show();
    }

    void form2_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Visible = true;
    }

    //Handler for the event from form 2
    void handleUpdateEvent(object sender, EventArgs e)
    {
        this.BackColor = Color.Red;
    }
}

Form2:

public partial class Form2 : Form
{
    //Declare your event
    public event EventHandler updateEvent;

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //If the event is registered fire it, otherwise do nothing
        if (updateEvent != null)
        {
            //fire the event and give our custom event args some text
            updateEvent(sender, e);
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //Another way to close the form, beside clicking the red "X"
        Close();
    }
}

Clicking the button on Form1 causes form1 to create form2, register its events, hide iteself, and show form2. When the "update button" (button1 of form2) is clicked, it will do some updating on Form1. For this demo, I just change Form1's backcolor. When form2 is closed its closed event will fire back to form1, which will handle the event by making itself visible again.

Solution 2

You have to use FormClosing event. In your code where you are creating form2 attach your form1 to closing event. This why your form1 will know form2 is being closed.

private void OpenForm()
{
     var form2 = new Form2();
     form2.FormClosing += FormIsClosing;
     form2.Show();

     this.Hide();
}

private void FormIsClosing(object sender, FormClosingEventArgs e)
{
     if (e.Cancel)
     {
         return;
     }

     this.Show();
     this.Update();
}
Share:
16,853

Related videos on Youtube

Matey
Author by

Matey

Updated on September 14, 2022

Comments

  • Matey
    Matey over 1 year

    i have 2 form. Form1 and Form2. In the form1 is a button, when I click it, then open the form2 and hide form1.

    But now, i dont know how to do, if I close Form2 and show form1.

    Question number 2: If I have in form method Update() for the elements in Form1 (labels, buttons). Can i use this method in form2 ?

    Thanks

  • user1703401
    user1703401 over 10 years
    Don't do this, Form2 should not have to know anything about Form1. Just put the FormClosing event handler in Form1. Don't forget to check e.Cancel. And note that you don't need to have the Owner set anymore either, it is just this.
  • gzaxx
    gzaxx over 10 years
    @HansPassant true, thank you very much. I'm not sure why I've done it other way around :(, but changed my code.