-1

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

Jerry Stratton
  • 2,699
  • 19
  • 25
Luke
  • 1,126
  • 2
  • 18
  • 25
  • 1
    Do you really want 3 different applications? Or just 1 application with 3 Forms? – Camilo Terevinto Dec 28 '17 at 17:45
  • thanks for quick comment @CamiloTerevinto , I need 3 applications caused it comes from 3 different vendors. – Luke Dec 28 '17 at 17:48
  • I guess you could fire up each form using `new Process` and implement single instance to bring up the other form. – zcui93 Dec 28 '17 at 18:01
  • @zcui93 : Main form have just been hidden, not closed, so I need to show it back, not another Main form instance. – Luke Dec 28 '17 at 18:03
  • 1
    Yes. Search for different single instance approaches. That's what you need. – zcui93 Dec 28 '17 at 18:04
  • @zcui93 thanks, looks good! I will try it at 8AM tomorrow and report with an answer! (it's 2AM now at my location) – Luke Dec 28 '17 at 18:26

3 Answers3

1

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.

Anirudha Gupta
  • 8,248
  • 8
  • 49
  • 71
0

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.

Rui Estreito
  • 262
  • 1
  • 10
  • thanks for feedback, I understand the error, you missed my point. I need some way to show Main form again after Program1 closed. – Luke Dec 28 '17 at 18:03
0

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);
        }
    }
    
Luke
  • 1,126
  • 2
  • 18
  • 25