2011-08-12 8 views
0

menustripのメニューをクリックすると、一度に1つのウィンドウしか許可されません。許可されたMenustrip瞬時に1人の子供

例:私はMenustrip Ordre、Tarifなどを持っています...初めてOrdreをクリックすると新しいフォームが開きますが、2回目はそれを許可しません。

private void ordresToolStripMenuItem_Click(object sender, EventArgs e) 
    { 

     if (Already open) 
     { 

     } 
     else 
     { 
      Lordre newMDIChild = new Lordre(ClientId); 
      // Set the Parent Form of the Child window. 
      newMDIChild.MdiParent = this; 
      // Display the new form. 
      newMDIChild.Show();     
     } 

    } 

事前

+0

したがって、2回目の動作は何ですか?メニュー項目を無効にする必要がありますか?フォームの既存のインスタンスをアクティブにする必要がありますか? –

答えて

1

のおかげで、あなたのフォームは初回のみを作成することにしたいし、同じ形式のメニュー項目が選択されている次回は、このような何かが仕事ができることが示された場合:

public class MyForm 
{ 
    private Window _openedWindow; 
    private void ordresToolStripMenuItem_Click(object sender, EventArgs e) 
    { 

     if (_openedWindow != null && _openedWindow.Open) 
     { 
      // 
     } 
     else 
     { 
      Lordre newMDIChild = new Lordre(ClientId); 
      _openedWindow = newMDIChild; 
      // Set the Parent Form of the Child window. 
      newMDIChild.MdiParent = this; 
      // Display the new form. 
      newMDIChild.Show();     
     } 
    } 
} 

これはBで書かれました:たぶんこのような何かが

private Lordre orderForm = null; 
private void ordresToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
    if (orderForm == null) 
     orderForm = new Lordre(ClientId); 
     // Set the Parent Form of the Child window. 
     orderForm .MdiParent = this; 

    } 
    // Display the form. 
    orderForm.Show(); 
    orderForm.Activate(); 
} 
0

rowserと私は長い時間のためのWindowsアプリケーションを書いていないので、正確なクラスとプロパティが異なる可能性があります。

0

通常、単一インスタンスのフォームを処理する方法は、メンバー変数を保持し、それがnullかどうかを確認することです。そう

、メンバ変数があります:あなたはそれがnullだかどうかを確認するチェックされている

private TestForm myTestForm = null;

、その後に、そうでない場合は、フォームを作成してメンバー変数に割り当て、子フォームのclosingイベントのイベントハンドラにアタッチします。

if (myTestForm != null) 
{ 
    MessageBox.ShowDialog("Sorry, you already have a TestForm open!"); 
} 
else 
{ 
    myTestForm = new TestForm(); 
    myTestForm.FormClosing += myTestForm_FormClosing; 
    myTestForm.MdiParent = this; 
    myTestForm.Show(); 
} 

と終了ハンドラでは、nullに戻すだけです。

private void myTestForm_FormClosing(Object sender, FormClosingEventArgs e) 
{ 
    myTestForm = null; 
} 

も、私は、検索のビットを行なったし、むしろFormClosingイベントハンドラを持つよりも、あなただけであるためにあなたの条件を変更することができます。

if ((myTestForm != null) && (!myTestForm.IsDisposed()) 
0

おかげで、あなたのすべてをあなたの応答を。

私はそれは私が欲しいものですが、それはあまりにも多くのコードである。この1

private void ordresToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     // a flag to store if the child form is opened or not 
     bool found = false; 
     // get all of the MDI children in an array 
     Form[] charr = this.MdiChildren; 

     if (charr.Length == 0)  // no child form is opened 
     { 
      Lordre myPatients = new Lordre(); 
      myPatients.MdiParent = this; 
      // The StartPosition property is essential 
      // for the location property to work 
      myPatients.StartPosition = FormStartPosition.Manual; 
      myPatients.Location = new Point(0,0); 
      myPatients.Show(); 
     } 
     else  // child forms are opened 
     { 
      foreach (Form chform in charr) 
      { 
       if (chform.Tag.ToString() == "Ordre") // one instance of the form is already opened 
       { 
        chform.Activate(); 
        found = true; 
        break; // exit loop 
       } 
       else 
        found = false;  // make sure flag is set to false if the form is not found 
      } 

      if (found == false)  
      { 
       Lordre myPatients = new Lordre(); 
       myPatients.MdiParent = this; 
       // The StartPosition property is essential 
       // for the location property to work 
       myPatients.StartPosition = FormStartPosition.Manual; 
       myPatients.Location = new Point(0,0); 
       myPatients.Show(); 
      } 
     } 
    } 

を発見しました。私はそれを減らすことができますか?

私はこれをそれぞれのストリップメニューに付ける必要があります。

if (chform.Tag.ToString() == "Ordre") 

if (chform.Tag.ToString() == "Another one") 
関連する問題