Search code examples
c#mdi

How to limit the number of open forms?


I have a Main form, which is MDI, and it has many child forms.

I want to limit the number of forms that can be open at once - e.g. no more than 8 forms open at a time.

How can I accomplish this?


Solution

  • If you're using MDI properly, then just check the Length of the MdiChildren collection in the Main MDI Form:

    private void button1_Click(object sender, EventArgs e)
    {
        if (this.MdiChildren.Length < 8)
        {
            Form child = new Form();
            child.MdiParent = this;
            child.Show();
        }
    }