2013-04-03 62 views
0
public partial class MainMenu : Form 
{ 
    public MainMenu() 
    { 
     InitializeComponent(); 
    } 
    windowOne One; 
    windowTwo Two; 
    private void OneToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     if (One != null) 
     { 
      One.WindowState = FormWindowState.Normal; 
      One.Focus(); 
     } 
     else 
     { 
      One = new windowOne(); 
      One.MdiParent = this; 
      One.FormClosed += (o, ea) => One = null; 
      One.Show(); 
     } 
    } 

    private void TwoToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     if (Two != null) 
     { 
      TwoWindowState = Two.Normal; 
      Two.Focus(); 
     } 
     else 
     { 
      Two = new windowTwo(); 
      Two.MdiParent = this; 
      Two.FormClosed += (o, ea) => Two = null; 
      Two.Show(); 
     } 
    } 

林初學者在c#, IM在窗口應用程序的工作,我希望當windowOne是打開的用戶couldnot打開windowTwo,使用上述碼即時通訊以避免打開windowOne或windowTwo agian。限制開口窗B時的窗口A是開放

+0

沒有但僅滿足一個要求,但我希望當一個窗口被打開,窗戶無法打開,而且,我不知道.. – rummykhan

回答

1

您可以禁用或者根據其窗口被打開的那一刻,然後再次啓用它。曾經關閉該窗口菜單項1/2

menuItem1.Enabled = false; 
+0

是另一個更好的方法..但你認爲什麼不會造成複雜.. ?? – rummykhan

+0

其最簡單的方法來禁用菜單項取決於選擇哪個窗口 –

+0

是的,我也有同樣的感覺。thanx bro – rummykhan

0

在打開之前檢查兩個窗口,然後在打開其中一個時關注正確的窗口。就像這樣:

if (One != null) 
{ 
    One.WindowState = FormWindowState.Normal; 
    One.Focus(); 
} 
else if (Two != null) 
{ 
    Two.WindowState = FormWindowState.Normal; 
    Two.Focus(); 
} 
else 
{ 
    // Show the window 
} 

編輯:由於這些代碼塊是完全一樣的它會更好地將它們剔除到自己的方法,雖然。

public bool IsWindowOpen() 
{ 
    if (One != null) 
    { 
     One.WindowState = FormWindowState.Normal; 
     One.Focus(); 
    } 
    else if (Two != null) 
    { 
     Two.WindowState = FormWindowState.Normal; 
     Two.Focus(); 
    } 
    else 
    { 
     return false; 
    } 
    return true; 
} 

然後,您可以使用它,像這樣:

if (!IsWindowOpen()) 
{ 
    // Open window 
    Two = new windowTwo(); 
    Two.MdiParent = this; 
    Two.FormClosed += (o, ea) => Two = null; 
    Two.Show(); 
} 
+0

很好的選擇,我從來沒有想過這個問題..感謝名單 – rummykhan

+0

它工作thanx ..兄弟 – rummykhan

+0

沒問題,不要忘記接受答案時,你可以點擊刻度。 –