Click here to Skip to main content
14,382,566 members
Rate this:
Please Sign up or sign in to vote.
See more:
I've created a new MDI parent form. I've set MDI Container of that form to True. I've created a menu strip. While clicking on the menu strip an MDI child has to be shown. The MDI Child has to be shown only if a condition is true. So I added the piece of code in the MDI Child - Form Load event handler like this,

private void Form_Load(...)
  if(condition == false)


But unfortunately, the entire application closes instead of the child form alone. Any solutions please?

What I have tried:

I've tried using this.Dispose() as well. That doesn't work too. If I use this.Close() there is no exception that is generated, while using this.Dispose() it throws an exception saying that : "Cannot show Disposed object".

Any ideas?
Updated 26-Jun-16 8:40am
Rate this:
Please Sign up or sign in to vote.

Solution 2

I think your app is shutting down with an error of type:

"An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll"

Because you are trying to Close the Form being created (child Form) in the Load Event where the Application is creating a Handle.

You should be seeing this information also:

"Additional information: Value Close() cannot be called while doing CreateHandle()."

If you are not seeing this type of error message, then you something else is wrong.

As Dave K. suggests in his solution, the answer here is never execute the code that creates the child Form unless the condition is satisfied.
Kiran Vaidyanathan 26-Jun-16 23:18pm
Nope Bill! There is no error, no message! The application just closes.
BillWoodruff 27-Jun-16 1:29am
What are you using to code in ? Visual Studio ?
Kiran Vaidyanathan 27-Jun-16 1:49am
Yes Bill!

C#, Visual Studio 2013, .NET Framework 4
Rate this:
Please Sign up or sign in to vote.

Solution 1

The Form_Load event is NOT the place to do this. You should be checking for this condition before you even try to create an instance of the child form.
BillWoodruff 26-Jun-16 13:40pm
Kiran Vaidyanathan 26-Jun-16 23:16pm
I assume that is the only solution to the problem? Is there any way to "avoid" the loading of a form during Form_Load() event handler? As of now, I've implemented like you have said.

Like I said earlier, I'll be creating my Form Object only on menu item click. So I've provided the condition check in there. If the condition satisfies, the object is created otherwise it is not created!

Thank you for your help! :)
Dave Kreskowiak 26-Jun-16 23:19pm
Yes, it's the only solution to the problem.
Kiran Vaidyanathan 26-Jun-16 23:21pm
Thank you Dave! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100