C# form switch and vice versa

276 views Asked by At

Assume that I have a C# Solution with 3 projects Main, Program1, Program2.
I want to have a "Main form", when I click on button "Program1" the main form will be hidden, Program1 will be showed, and when I close Program1, the Main form will return.
How can I do this? I tried add Program1 and PRogram2 as Reference to Project Main and code like below in Main, it works for call Program1, but can't handle event Program1.closed() because when I try to reference Main to Program1, it error

---------------------------
Microsoft Visual Studio
---------------------------
A reference to 'Main' could not be added. Adding this project as a reference would cause a circular dependency.
---------------------------
OK   
---------------------------

I searched Google and got nothing helpful!

using System;
using System.Windows.Forms;

namespace Switch
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Program1.Form1 pf1 = new Program1.Form1();
            pf1.Show();
            this.Hide(); 
        }
    }
}

my solution my main form, quite simple

3

There are 3 answers

0
Luke On BEST ANSWER

Thanks everyone for answers!
After confirmed with customer, they don't strictly need the "mainform" to be hidden, so I came with another easier solution:
1. For the "child form", I use ShowDiaglog() instead of Show()

    private void btnChildForm1_Click(object sender, EventArgs e)
    {
        var frm = new ChildForm1();
        frm.ShowDialog();
    }
  1. For the mainform, I use mutex to force it to be only 1 instance:

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
    
    
        [STAThread]
        static void Main()
        {
            var mutex = new Mutex(true, "MainForm", out var result);
            if (!result)
            {
                MessageBox.Show("Running!");
                return;
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
            GC.KeepAlive(mutex);
        }
    }
    
1
Rui Estreito On

Circular dependencies use to happen when the project arquitecture is not good. In your case i think the problem migth be the program1 or program2 have Main as a reference. Remove de Main reference from the program1 and program2. The main project must have reference to the program1 and program2.

0
Anirudha Gupta On

As zcui93 commented you can use process to make it work. You can either have all 3 in same folder (when you deploy the app on client machine)

using System.Diagnostics;
...
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = "-n";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
process.WaitForExit();// Waits here for the process to exit.

In C# you can use the Process.Exited event. This event doesn't work when someone close the app when someone kill the app from task manager.