将代码翻译成类

时间:2013-08-18 18:43:34

标签: c# winforms class

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

3 个答案:

答案 0 :(得分:1)

好的这次,扩展方法怎么样?我假设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

的新类中

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

如果您遇到问题,请告诉我,我可以在编译器前面了解这一点。

答案 1 :(得分: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)

答案 2 :(得分: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我们检查:

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

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