2

我的 MDI 父母有这个:

Form1 newForm1 = new Form1(); //newForm1 is the instance of Form1
private void MDIParent1_Load(object sender, EventArgs e)
{
    newForm1 = null; //newForm1 is set to null
}

private void toolStripButton1_Click(object sender, EventArgs e)
{
    if (newForm1== null)
    { 
        // if Form1 is not yet open it will be open 
        newForm1 = new Form1();
        newForm1.MdiParent = this;
        newForm1.FormClosed += new FormClosedEventHandler(newForm1_FormClosed); //add event handler when the form close
        newForm1.Show();
    }
    else
        //if Form1 is already open it will just be activate
        newForm1.Activate();
}

void newForm1_FormClosed(object sender, FormClosedEventArgs e)
{
    newForm1 = null; //when the Form1 Closed the newForm1 will be set to null
}

我将如何将其翻译为课程?它会是这样的:

public static void openForm(Form newForm, FormInstance Instance) //newForm is the name of the Form, Instance is the instance of that form
{
    if (Instance == null)
    {
        Instance = new newForm();
        Instance.MdiParent = this;
        Instance.FormClosed += new FormClosedEventHandler(Instance_FormClosed);
        Instance.Show();
    }
    else
        Instance.Activate();
}   

void Instance_FormClosed(object sender, FormClosedEventArgs e)
{
    Instance = null;
}

所以我会有这个:

Form1 newForm1 = new Form1(); //newForm1 is the instance of Form1
Form2 newForm2 = new Form2(); //newForm2 is the instance of Form2
private void MDIParent1_Load(object sender, EventArgs e)
{
    newForm1 = null; //newForm1 will set to null
    newForm2 = null; //newForm2 will set to null
}

private void toolStripButton1_Click(object sender, EventArgs e)
{
    openForm(Form1, newForm1); //Form1 is the name of the Form, newForm1 is the instance of Form1
}

private void toolStripButton2_Click(object sender, EventArgs e)
{
    openForm(Form2, newForm2); //Form2 is the name of the Form, newForm2 is the instance of Form2
}
4

3 回答 3

1

好的,这次,扩展方法怎么样?我假设Form1并且Form2都是System.Windows.Forms.Form

using System.Windows.Forms;

public static class Extensions
{
    public static void OpenForm<T>(this T frm, Form parent) where T : Form, new()
    {
        if (frm != null && FormOpen(frm.Text))
            frm.Activate();
        else
        {
            frm = new T();
            frm.MdiParent = parent;
            frm.FormClosed += (sender, args) => {frm.Dispose(); frm = null;};
            frm.Show();
        }
    }

    private static bool FormOpen(string name)
    {
        FormCollection fc = Application.OpenForms;

        foreach (Form frm in fc)
        {
            if (frm.Text == name)
                return true;
        }
        return false;
    }
}

把它扔进一个叫做的新类Extensions.cs

并调用它(例如: on Form1newform1.OpenForm(this)

如果您有问题,请告诉我,我可以在编译器面前解决这个问题。

于 2013-08-18T19:51:10.063 回答
0

也许使用泛型?我不完全明白你想做什么

public static void openForm<T>(T Instance)
{
    if (Instance == null)
    {
        Instance = new T();
        Instance.MdiParent = this;
        Instance.FormClosed += new FormClosedEventHandler(Instance_FormClosed);
        Instance.Show();
    }
    else
        Instance.Activate();
}

//This was inside the openForm method (which made no sense to me) so I moved it out.
void Instance_FormClosed(object sender, FormClosedEventArgs e)
{
   Instance = null;
}

那么你会称之为

openForm<Form1>(newForm1)
于 2013-08-18T19:14:39.690 回答
0

如果您想从主类/表单打开另一个表单并且您只想打开表单的一个实例,请尝试下一种方法:

以另一种形式创建静态成员:

Class AnotherForm1
{
    private static AnotherForm1 Instance;

    //creating a static function for creating/getting Instance
    public static AnotherForm1 CreateInstance()
    {
        if (AnotherForm1.Instance == null) 
        { 
             AnotherForm1.Instance = new AnotherForm1(); 
        }
        return AnotherForm1.Instance;
    }

    //Constructor was made private because we want 
    //that our form instance willbe creating only through CreateInstance function
    private AnotherForm1()
    {
        this.FormClosed += new FormClosedEventHandler(Instance_FormClosed);
    }

    //Here we set reference to Instance to null when form are closed
    private void AnotherForm1_FormClosed(object snder, FormClosedEventArgs e)
    {
        AnotherForm1.Instance.Dispose();
        AnotherForm1.Instance = null;
    }
}

然后在主要形式:

private void toolStripButton1_Click(object sender, EventArgs e)
{
    AnotherForm1 form1 = AnotherForm1.CreateInstance();
    form1.MDIParent = this;
    form1.Show(); 
}

所以CreateInstance我们检查:

如果我们的表单实例已经创建 -> 然后返回对它的引用,

如果不是 -> 然后创建新的并返回对它的引用

于 2013-08-18T19:58:21.733 回答