Accessing Windows Form from multiple commands

I need a Windows form that is accessible from multiple commands and forms within my plugin.

Ex:

The first form, we’ll call it form1, shows a “view” button to show the second form, after calculations have been made by the various commands

The second form, we’ll call it form2, has ListBox1 and ListBox2.

The first command runs many calculations and adds the results to the ListBox1.Items collection on form2

The second command runs other calculations and adds the results to the ListBox2.Items collection on form2

The user clicks the “view” button on form1 and form2 appears, showing the results from the two commands.

I would think the two forms would be instantiated in the plugin file, but when I do that with form2 and try to reference it from form1 by calling Plugin.form2.Show(), the compiler complains that Plugin does not contain a definition for form2.

You could make a singleton form, by creating a custom constructor that encapsulates the creation of the form.
Something along the lines of

public class Form2Singleton
{
    private static Form2Singleton instance = null;
    private Form2 form2 = null;

    private Form2Singleton()
    {
        form2 = new Form2();
    }

    public static Form2Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Form2Singleton();
            }
            return instance;
        }
    }

    public Form2 Form2
    {
        get
        {
            return form2;
        }
    }
}
Form2Singleton.Instance.Form2.ListBox1.Items.Add("Item 1");
1 Like