I'm looking for a good pattern to resolve the following circular reference in a Windows Form application:
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?
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();
}
}
Have you considered creating a 3rd assembly and moving all the common code into the 3rd assembly?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With