Search code examples
c#winformsmenustrip

Allowed Menustrip only one child at moment


How can i allow only one window at a time when i click menu in menustrip ?

Ex: i have Menustrip Ordre, Tarif, etc... when i click Ordre for the first time it will open a new form, but for the second time i want to disallow it.

 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


Solution

  • If you want the form to be created only the first time, and then show that same form the next time the menu item is selected, something like this could work:

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