将代码转换为类

本文关键字:转换 代码 | 更新日期: 2023-09-27 18:28:02

我的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
}

将代码转换为类

这次好了,扩展方法怎么样?我假设Form1Form2都是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 的新类中

并称之为(例如:在Form1上)newform1.OpenForm(this)

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

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

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)

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

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

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中,我们检查:

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

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