0

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?

Community
  • 1
  • 1
DhavalR
  • 1,409
  • 3
  • 29
  • 57
  • possible duplicate of [How can I close a login form and show the main form without my application closing?](http://stackoverflow.com/questions/4759334/how-can-i-close-a-login-form-and-show-the-main-form-without-my-application-closi) – Cody Gray - on strike Mar 12 '13 at 12:49

1 Answers1

0

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.

andyhasit
  • 14,137
  • 7
  • 49
  • 51
  • Yes. The mainform is my LoginForm. But it's not MdiParentForm. MdiParent opens after successfull login. And your third explanation: I tried that e.g. Application_Name.MdiParentForm_Name. But did not work. Shows error "Application_Name.MdiParentForm_Name is a type, which is not valid in the given context." – DhavalR Mar 12 '13 at 12:50
  • I would change your application to have the MdiParent form as main form, which is hidden to start with until the user successfully completes Login, after which you close the Login form. I'm not sure where you're getting Application_Name from? That's not what I was suggesting. Application.OpenForms is a collection of OpenForms, OpenForm has a property Name. Perhaps edit your question to illustrate what you are trying to achieve? – andyhasit Mar 12 '13 at 15:30
  • Actually I wanted to close the login form. That was the primary goal that is achieved. Now the question is how to set the MdiParent of a form (i.e. Form2) which is opened from Form1 that is child form. I am closing Form1 and opening Form2. The main goal of the question is solved (i.e. To close Login_Form that is being showed in Application.OpenForms[index]). But as you said "iterate through them and select right one by name", how to use Name property of OpenForms? – DhavalR Mar 13 '13 at 17:11
  • Edited answer to show source for iterating through OpenForms. – andyhasit Mar 14 '13 at 10:24