Closing an opening window

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

I need to:
a) Close a windows form from within the form's code itself
b) It must be able to occur after the form's constructor function is called
(like from another function that's called before .Show())
c) It must be able to occur before the form is actually viewable.

It's for security purposes.

If I add it to load, I get an error that you can't call close() or dispose()
while creating the window handle. I also tried doing it in the handlecreated
event for that reason, but I get the same error.

Any ideas (other than not calling .Show())?
 
I would like to know that as well but I found a suitable work around in my
case that may be of help. I created a form property that was set after the
InitializeComponent() and tested it's value in the LOAD() of the form. If
it wasn't what I wanted it would close the form before anyone could use it.


private void MainForm_Load(object sender, System.EventArgs e)

{

if (this.isLoggedIn == true)

{

}

else

{

this.Close() ;

}


}
 
See, but when I do that, I get the error that you can't close the form while
creating the window handle. I can't call close or dispose in load without
getting the error.
 
Thats odd, it works for me. I copied straight from my form that I am
currently messing with. I will take a further look and see if anything
comes to mind.
 
I am not up yet on the way C# handles inheritance but in my case my form was
based directly on the Form class. Is yours based on a subclass? If so make
sure the Load() of the base class (or what ever it's called din C#) is
called before you issue the Close().
 
Cant you just create a public procedure in your form. Then, instead of
calling
..show(), call it. For instance:

private class Form1 : Form
{
....

public void CheckThenShow()
{
//check to see if person is allowed to see form
if (Allowed)
this.show();
}
....
}
 
Nevermind. I reread your post and saw that you did not want to call a
different method than .show()
 
Back
Top