2011-08-12 23 views
0

我怎麼能一次只允許一個窗口,當我點擊menutrip菜單?允許Menustrip在此刻只有一個孩子

例如:我有Menustrip Ordre,Tarif等...當我第一次點擊Ordre時,它會打開一個新窗體,但是第二次我想禁止它。

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

那麼,你第二次想要什麼行爲?菜單項是否應該禁用?它是否應該激活已經存在的表單實例? –

回答

1

如果你想創建的形式僅在第一次,然後顯示相同的形式選擇菜單項接下來的時間,這樣的事情可能工作:

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

也許是這樣的:

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寫rowser和我還沒有寫很長時間的Windows應用程序,所以確切的類和屬性可能會有所不同。

0

我通常處理單實例窗體的方法只是有一個成員變量來保存它,然後檢查它是否爲空。

所以,有一個成員變量:

private TestForm myTestForm = null;

,然後當你正在檢查只是檢查它是否是空;如果沒有,當你創建你的表單時,將它分配給你的成員變量,並附加到子表單關閉事件的事件處理程序。

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(); 
} 

並在關閉處理程序中,只需將其設置爲空。

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

還,我做了一些搜索的而非具有的FormClosing事件和處理程序,你可以改變你的條件是:

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

感謝大家的響應。

我發現這一個

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(); 
      } 
     } 
    } 

這是我想要的,但它是太多的代碼。我想知道我能否減少它。

我必須使這個我的每個stripmenu。

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

if (chform.Tag.ToString() == "Another one")