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

0

メイン クラス/フォームから別のフォームを開き、フォームのインスタンスを 1 つだけ開きたい場合は、次の方法を試してください。

別のフォームで静的メンバーを作成します。

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 に答える
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 に答える