'how to hide form1 by clicking button in form2

I designed a form for login that named as form1, and have 2 more form form2,form3 form2 items showing in panel from form1 and what I want to do when I click the button in panel ( the item from form2 ) want to show form2 and hide form1 but the code isnt working

    private void button1_Click(object sender, EventArgs e)
    {
    Form1 frm1 = new Form1();
    Form3 frm3 = new Form3();
    frm1.Hide();
    frm3.Show();
    };

form3 is opening but form1 isnt hiding



Solution 1:[1]

Its not hiding because you created a new instance for form1 which is already instantiated. You must call the Hide() method on the same instance used to call the Show() method. If you added this code inside form1 class ,then change it like this

private Form1 frm1

public Form2()
{
frm1 = new Form1()
}

private void button_show_form1_Click(object sender, EventArgs e)
    {

    frm1.Show();   

    };

private void button1_Click(object sender, EventArgs e)
    {

    Form3 frm3 = new Form3();
    frm3.Show();

    frm1.Hide();

    };

Solution 2:[2]

You creating both forms in your codesnipped. Form1 is not the form you want to close, i think. frm1 is only another instance of Form1, but not the openend instance von Form1. You must have anywhere another instance of Form1. You must use the right referenz to the right instance.

Solution 3:[3]

It is important to know that WinForms create an instance of the startup form. In our case the startup form would be Form1. So, when you say

Form1 frm1 = new Form1();

You're actually creating a new (second) instance of Form1. This means that, in code, there are two different Form1's.

What we want to do is check with our application to get the instance of Form1 that already exists.

// This goes in Form2. It returns an instance of Form1, if it exists.
private Form getForm1()
    {
        // Application holds information about our application, such as which forms are currently open.
        var formCollection = System.Windows.Forms.Application.OpenForms;
        // Now we loop through the open forms in search of the form we want, Form1.
        foreach (Form frm in formCollection)
        {
            if (frm.Name.Equals("Form1"))
            {
                return frm;
            }
        }

        return null;
    }

Now that we can get the existing instance of Form1 we can use it to make the form hidden.

private void button1_Click(object sender, EventArgs e)
{
    var form1 = getForm1();
    if (form1 != null) form1.Hide();
}

Something to know here is that when a form is hidden it is not closed. So, we need to make sure that Form1 becomes visible again. For example, we can set Form1 to be visible when Form2 closes.

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    // The question mark (?) checks to see if the result of
    // getForm1() is null. Same thing that is happening in
    // button1_click
    getForm1()?.Show();
}

Complete Form2 Code

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

    private void button1_Click(object sender, EventArgs e)
    {
        var frm1 = getForm1();
        var frm3 = new Form3();
        if (frm1 != null) frm1.Hide();
        frm3.Show();
    }

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        // The question mark (?) checks to see if the result of getForm1() is null. Same thing that is happening in button1_click
        getForm1()?.Show();
    }

    // This goes in Form2. It returns an instance of Form1, if it exists.
    private Form getForm1()
    {
        // Application holds information about our application, such as which forms are currently open.
        // Note that Open and Visible have different definitions.
        var formCollection = System.Windows.Forms.Application.OpenForms;
        // Now we loop through the open forms in search of the form we want, Form1.
        foreach (Form frm in formCollection)
        {
            if (frm.Name.Equals("Form1"))
            {
                return frm;
            }
        }

        return null;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Taladan
Solution 3 bPuhnk