Closing a form on the Load event?

  • Thread starter Thread starter David Smith
  • Start date Start date
D

David Smith

Under a certain circumstance, I want a form to display a messagebox in the
load event, and then close. But apparently I cant close a form in the load
event. Any ideas?

Thanks
 
Maybe you could check for this circumstance before calling Application.Run()
or Form.Show() if it's not the main form.

Best Regards, Mikael
 
I agree with John, it seems poorly structured.

However, that being said, it worked fine for me:
private void Form1_Load(object sender, System.EventArgs e)
{
Console.WriteLine("Form Load");
MessageBox.Show("Hi");
this.Close();
}


A better solution:
Add a method to your Form called:
public bool ShouldShow()
And use that before calling Show().

Or you could try using the VisibleChanged event. Unfortunately, in an
example I ran, the VisibleChanged event fires after the Load event

Finally, you could declare a new Show() method that either display a
message box or else calls base.Show(). Note, however, that Show is
not declared virtual, so Application.Run(myForm) will not call your
method polymorphically.

mike
 
Hi David,

David said:
Under a certain circumstance, I want a form to display a messagebox in the
load event, and then close. But apparently I cant close a form in the load
event. Any ideas?

My idea is to call "Application.Exit()"
and it works...

Marcin Grzêbski
 
Marcin Grzêbski said:
My idea is to call "Application.Exit()"
and it works...

Only if you want to completely exit the application. What about if
you just want to stop showing some child form?
 
mist said:
It's a bug of framework 1.0,you can use framework 1.1,
it's worked fine on framework1.1:

But I'm using framework 1.0 and everything worked fine for me.
-mike
 
Back
Top