Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular Reference

I'm looking for a good pattern to resolve the following circular reference in a Windows Form application:

  • Assembly 1 contains a Windows Form with an Infragistics menu item ".Show"ing a Form in Assembly 2
  • Assembly 2 contains a Windows Form with an Infragistics menu item ".Show"ing a Form in Assembly 1

The menu has generally the same items on it throughout the application. So both Assembly 1 and Assembly 2 have references to one another to "New up" one anothers' forms and .Show them.

A note about size: My app is an existing app, so the situation is not quite as simple as the above two-assembly situation. But if I can solve the above simply (probably not implementing a , I can apply that to a much larger application (about 20 components, all with several forms that pop each other up across components).

I've thought through a few solutions, but they all seem cumbersome. Is there a simple solution I'm missing?

like image 339
Mark A Johnson Avatar asked Aug 27 '26 12:08

Mark A Johnson


2 Answers

You could (in both cases) make the button simply raise an event. The shell exe references both assemblies, and hooks up the even to show the other form.

So the exe knows about both; neither of the forms knows about the other.

For example (same concept):

using System;
using System.Windows.Forms;
class Form1 : Form
{
    public event EventHandler Foo;
    public Form1()
    {
        Button btn = new Button();
        btn.Click += delegate { if(Foo!=null) Foo(this, EventArgs.Empty);};
        Controls.Add(btn);
    }
}
class Form2 : Form
{
    public event EventHandler Bar;
    public Form2()
    {
        Button btn = new Button();
        btn.Click += delegate { if (Bar!= null) Bar(this, EventArgs.Empty); };
        Controls.Add(btn);
    }
}
static class Program
{
    [STAThread]
    static void Main()
    {
        ShowForm1();
        Application.Run();
    }
    static void ShowForm1()
    {
        Form1 f1 = new Form1();
        f1.Foo += delegate { ShowForm2(); };
        f1.Show();
    }
    static void ShowForm2()
    {
        Form2 f2 = new Form2();
        f2.Bar += delegate { ShowForm1(); };
        f2.Show();
    }
}
like image 104
Marc Gravell Avatar answered Aug 30 '26 01:08

Marc Gravell


Have you considered creating a 3rd assembly and moving all the common code into the 3rd assembly?

like image 30
John Sonmez Avatar answered Aug 30 '26 01:08

John Sonmez