Technical Journal

Programming, software technologies, operating systems, IT

Canceling the Load Form event in .NET

I was trying to find a way not to load a form when some error condition occured.

Briefly, the whole cancel logic should be placed in the constructor of the form and an exception should be thrown whenever canceling is necessary. The Show or ShowDialog methods must be included in the same try-catch block as the constructor.

Thus, when the exception is thrown, no form is displayed. There is no reliable way of canceling the Load event from within the Load event handler : if you call Close( ), you get an exception of the type : “Cannot acces a disposed object” on the Show or ShowDialog form methods.

See the following discussion also :

http://discuss.fogcreek.com/dotnetquestions/default.asp?cmd=show&ixPost=1286.

July 20, 2007 Posted by andocs | Programming, Windows | | No Comments Yet