0
votes

This is my first question so I hope I am doing this right. I am currently using Visual Studio Express 2017 doing a C# Windows Forms App project.

I have searched through Stack Overflow and YouTube for a similar examples but could find none. In the image attached, I have 3 forms in the project. Form1 which is the main controlling form has 2 buttons on it. Form2button for Form2 and Form3button for Form3. When I click From2button, I can keep clicking and open multiple Form2's as well open multiple Form3's without limitations. I would like to run the program that if I click on Form2button it can only load one instance of Form2 and the same with Form3button.

However if I click Form3button and Form2 is open, I want Form2 to close and only have Form3 open as well as is Form3 is open and I click on Form2button, Form3 will close and Form2 will open. All the time Form1 is active on the screen.

Overview of Form Program

namespace MultiWindows

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

    private void Form2button_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
    }

    private void Form3Button_Click(object sender, EventArgs e)
    {
        Form3 f3 = new Form3();
        f3.Show();
    }
}

}

1

1 Answers

0
votes

I have managed to get this to work so far with help from other people. I am not taking credit for their help.

So when I run the program and click on Form2button it will open Form2 as well as vica versa for Form3. When I click on Form3button it will not close Form2 and only have Form3 on the screen so both forms are active on the screen.

If I close a form by clicking in the upper right corner I cannot reopen the Form that was closed unless I restart the program.

namespace MultiWindows {

public partial class Form1 : Form

{
    public Form1()
    {
        InitializeComponent();
    }
    private Form2 f2 = null;
    private Form3 f3 = null;

    private void Form2button_Click(object sender, EventArgs e)
    {
        if (this.f2 == null)
        {
            f2 = new Form2();
            f2.Show();
        }
        else
        {
            f2.BringToFront();
        }
    }

    private void Form3Button_Click(object sender, EventArgs e)
    {
        if (this.f3 == null)
        {
            f3 = new Form3();
            f3.Show();
        }
        else
        {
            f3.BringToFront();
        }
    }
}

}