一次只允许菜单显示一个孩子



当我在菜单栏中单击菜单时,我怎么能一次只允许一个窗口呢?

例如:我有菜单栏订单,关税等…当我第一次点击订单时,它将打开一个新表单,但第二次我想禁止它。

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

Thanks you in advance

如果您希望仅在第一次创建表单,然后在下次选择菜单项时显示相同的表单,则可以这样做:

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

可以这样写:

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应用程序了,所以确切的类和属性可能会不同。

我通常处理单实例表单的方式只是有一个成员变量来保存它,然后检查它是否为空。

所以,有一个成员变量:

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

感谢大家的回复。

我找到了这个

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")

我的方法是首先将屏幕存储在类中的变量中,如下所示:

private Lordre _LordreChildForm = new Lordre(ClientID)

然后为它创建如下属性:

public Lordre LordreChildForm { get => _LordreChildForm; set => _LordreChildForm = 
value; }

然后在表单的form Closing事件中将该值设置为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();
}

相关内容

  • 没有找到相关文章

最新更新