2
Answers

Cannot re-open a form after it was closed

Ask a question
Henry Vuong

Henry Vuong

16y
13.6k
1
I have a parent form (Main) and a child form (form1). The parent form's IsMdiContainer is set to true. When I click on a menu item, the child form is opened within the parent form, no problem. But when I close the child form and try to re-open it by clicking on the menu item again, it does not show, and there's an error: "Invisible or disabled control cannot be activated."

It appears that form1 is not really closed, it's just hidden somewhere but somehow I cannot re-display it. I tried both form1.Show() and form1.Visible = true but nothing works. Here's my code:

public partial class Main : Form
    {
        Form1 form1;
        public Main()
        {
            InitializeComponent();
        }

        private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (form1 != null)
            {
                form1.Show();
                this.ActiveControl = form1;
                form1.WindowState = FormWindowState.Normal;
                form1.BringToFront();
                return;
            }
            else if (form1 == null || form1.IsDisposed)
            {
                form1 = new Form1();
                form1.MdiParent = this;
                form1.TopLevel = false;
                form1.Dock = DockStyle.Fill;
                form1.Show();
                this.ActiveControl = form1;
                form1.BringToFront();
            }
        }
    }

Answers (2)