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#的初学者, 我在窗口应用程序上工作,我希望当windowOne打开时用户无法打开windowTwo,我使用上面的代码来避免打开windowOne或windowTwo agian ..
答案 0 :(得分:1)
您可以禁用menuitem 1/2,具体取决于当时打开的窗口,然后关闭窗口后再次启用它
menuItem1.Enabled = false;
答案 1 :(得分: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();
}