0

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

yusuf
  • 7
  • 2
  • You need to get the instance of Form1 that already exists. The above code creates a new Form1 and immediately hides it. – hijinxbassist Apr 21 '22 at 20:50
  • Does this answer your question? [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) – JohnG Apr 21 '22 at 23:08

3 Answers3

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

    };
Jahury
  • 83
  • 14
  • _"...call the Hide() method on **the same instance** used to call the **Show()** method..."_ - do you mean `button1_Click`? –  Apr 21 '22 at 22:19
  • thats not work becuase the form is form2 I want to hide form1 by clicking button in form2 – yusuf Apr 21 '22 at 22:28
  • declare form1 as global then instantiate it in the constructor, hence you call show and hide the form freely using the same instance. – Jahury May 10 '22 at 14:38
0

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.

Taladan
  • 429
  • 1
  • 8
  • 20
  • I'm new at coding Idk how to do if possible can you send me a code sample? – yusuf Apr 21 '22 at 20:56
  • First, i think, you work with windows forms. it is not state of the art. There are many vidoes on youtube, if you use the right search words. `windows forms close window` – Taladan Apr 21 '22 at 21:05
0

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;
    }
}
bPuhnk
  • 375
  • 2
  • 11
  • public static Form getForm(string form) { var formCollection = Application.OpenForms; foreach (Form frm in formCollection) { if (frm.Name.Equals(form)) { return frm; } } return null; } Is in my opinion the better way to solve it, because you could use it for multiple forms. – mKeey May 15 '23 at 01:07