Search code examples
c#mdi

MDI Parent Form :- setting Parent (Maybe just the title is duplicate)


Look at the answer of this question. I have tried the second comment of the aceepted answer. The problem is with "Application.OpenForms[0]". It gives me error saying "Form that was specified to be the MdiParent for this form is not an MdiContainer. Parameter name: value". Then I expand to see how many forms are open. I got an amazing result which I DISCOVERED NOW. There are two open forms. One of them is LoginForm which I have this.Hide(); on successfully login. When I changed it to this.Close(); the application closed. Why it is still opened and How can I Close it without closing the application?


Solution

  • First, if the application is closing altogether when you close a form, it's most likely because you're closing the main form, i.e. the one specified in:

    Application.Run(new MainForm());
    

    Is your LoginForm that you are closing that main form?

    Second, if a form is to be an MdiParent it must have the property IsMdiContainer to true.

    Third, I wouldn't rely on Application.OpenForms[0], instead iterate through them and select the right one by name, like this:

    FormCollection fc = Application.OpenForms;
    
    foreach (Form frm in fc)
    {
    if (frm.Name == "Main Form")
        do what you need to do...
    }
    

    Hope this helps.