We help IT Professionals succeed at work.

Error trying to close main form in Windows Form project

Andrew Werber
on
After creating a Windows Form project I added Close(); after InitializeComponent(); in the constructor. When I run the project, i get the following error:

Make sure you have not released a resource before attempting to use it

The error happens in the Program.cs file.

My real code at work is a bit more complicated, what I'm doing is authenticating a user against a table and then trying to close to form if they're not in the table. But the functions I call are called from the constructor, and the error is the same error in the same place. I'd like to know how to close the form from the constructor, other than sinking the error. I'm concerned that there may be a memory leak.
Comment
Watch Question

Software Team Lead
Commented:
try call the Close() method in Form_Load event instead?
Kyle AbrahamsSenior .Net Developer

Commented:
You can check that before you call the constructor of the form.  Or the other option is to create a "Blackout Panel" (EG: a fully docked panel) and set that visible to true if they aren't authenticated / authorized.

Author

Commented:
Since this was the first answer, I tried it and it worked. Thanks!