当我点击menustrip中的菜单时,我怎么能一次只允许一个窗口?
Ex:我有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();
}
答案 1 :(得分: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();
}
}
}
这完全是在浏览器中编写的,我很长时间没有编写Windows应用程序,所以确切的类和属性可能会有所不同。
答案 2 :(得分:0)
我通常处理单个实例表单的方法就是拥有一个成员变量来保存它,然后检查它是否为null。
所以,有一个成员变量:
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();
}
并在结束处理程序中,只需将其设置为null。
private void myTestForm_FormClosing(Object sender, FormClosingEventArgs e)
{
myTestForm = null;
}
另外,我做了一些搜索,而不是使用FormClosing事件和处理程序,你只需将条件更改为:
if ((myTestForm != null) && (!myTestForm.IsDisposed())
答案 3 :(得分: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")
答案 4 :(得分:0)
我这样做的方法是,首先将屏幕存储在类似此类的变量中:
private Lordre _LordreChildForm = new Lordre(ClientID)
然后为它创建一个属性,如下所示:
public Lordre LordreChildForm { get => _LordreChildForm; set => _LordreChildForm =
value; }
然后在表单的“表单关闭”事件中将值设置为null,如下所示:
private void Lordre_FormClosing(object sender, FormClosingEventArgs e)
{
ClassName.LordreChildForm= null;
}
现在,终于可以在按钮单击事件处理程序中设置if语句,如下所示:
private void LordreToolStripMenuItem_Click(object sender, EventArgs e)
{
if(ClassName.LordreChildForm== null || ClassName.LordreChildForm.IsDisposed)
{
ClassName.LordreChildForm= new DeregisterClub();
ClassName.LordreChildForm.MdiParent = this;
}
LordreChildForm.Dock = DockStyle.Fill;
LordreChildForm.Show();
LordreChildForm.Focus();
}