Cancelling Form.Load event

  • Thread starter Thread starter Mark Baldwin
  • Start date Start date
M

Mark Baldwin

I load some data from the database during the forms load event. If this load
fails, then I need to bring up the error message and abort loading the form.

How do I do this in .NET 2.0? There doesn't seem to be a way of cancelling
the forms construction from within the load event. Also if an error occurs I
don't want a blank form displayed under the error message that then vanishes
after the user clicks OK.

Any help much appreciated...
 
Mark said:
I load some data from the database during the forms load event. If this load
fails, then I need to bring up the error message and abort loading the form.

How do I do this in .NET 2.0? There doesn't seem to be a way of cancelling
the forms construction from within the load event. Also if an error occurs I
don't want a blank form displayed under the error message that then vanishes
after the user clicks OK.

Any help much appreciated...
Hi, Mark

Why don't you load the data before loading the form ?

Regards,
Petar Atanasov
http://a-wake.net
 
Hi Mark,

IMHO, I don't think it is a good idea to cancel a form's construction from
within the load event handler. If you do want that, you can call the form's
Close method, which will close the form, rather than cancel the form's
construction.

A better solution to your question is to load data from the database before
you create the form. If the loading fails, display an error messge and
don't create the form.

If you're using C#, you could put the code to load data from database in
the static Main method within the Program.cs file, and before the statement
"Application.Run(new Form1())". If the loading fails, don't call this
statement.

If you're using VB.NET, you have two choices. If you select Sub Main as the
startup object , you could place the code to load data in the sub Main, and
before the line of code "Application.Run(New Form())". If the loading
fails, don't call this line of code.

If you choose a form as the startup object and enable the application
framework, you could put the code to load data in the application's Startup
event handler. If the loading fails, set the Cancel property of the
Microsoft.VisualBasic.ApplicationServices.StrartupEventArgs parameter to
true.

To handle the application's Startup event, right click the project in the
Solution Explorer and choose Properties. In the Project Designer, switch to
Application tab and click the 'View Application Events' button. In the
opened ApplicationEvents.vb file, select '(MyApplication.Events)' from the
top left combobox, and select 'Startup' from the top right combobox and a
stub of the Startup event handler will be generated.

Hope this helps.
If my suggestion is not appropriate to your practice, please feel free to
let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top