Configuring Restricted SystemsImpersonation Class

Application Events

May 16th, 2007

Application Events make VB.NET form programming a piece of cakeApplication Framework events, introduced for VB.NET in VS 2005/Framework 2.0, provide you ways to better manage your application by extending the My namespace through partial classes.

To enable the Application Framework in your WinForms application, go to the Application tab of “My Project”. Make sure the “Enable Application Framework” checkbox is checked. Then click on the “View Application Events” button in the frame below. This will create and show the ApplicationEvents module. Now you’re ready to create and use the event procedures.

The first event we’ll look at is the Startup event. This event is raised when the application starts and before the specified startup form is created. This allows you to execute code prior to any form loads. This is like the old Sub Main method but it acts as an extension of the My NameSpace. In this routine you can even show an optional setup dialog that you want seen before the main form in some cases. You can prevent the startup form from loading by using the Cancel property of the StartupEventArgs parameter.

The next event is the Shutdown event. This event allows you to run code while your application is in the process of shutting down. This event is good for saving final updates to the database, saving configuration information, or performing other final tasks. This is an ability that programmers coming from VB6 should particularly appreciate.

The UnhandledException event is another addition that VB6′ers will appreciate. Remember how you always asked how to have a ‘global’ error trap? Well, here it is. This event will fire whenever an untrapped error occurs in your program. While you should always manage the possibility of exceptions by writing good code and dealing with expected exceptions gracefully in your main code, this event does give your a safety valve to deal with truly unexpected situations. The UnhandledExceptionEventArgs parameter will give you the exception that happened and you can use it’s ApplicationExit property to either return to your application if you set this value to False or you can use True, the default, to exit the application at this point.

StartupNextInstance is an event that fires if you’ve set the “Make Single Instance Application” of your application to True. This allows you to manage what happens when somebody tries to start another instance. You can use the StartupNextInstanceEventArgs BringToForeground property to pop your app to the front, a nice feature for the user who may have forgotten they already had it running. You can also use the CommandLine property of this parameter to get the command line from the other instance that was started. This is very handy if your program uses command lines.

The last event is NetworkAvailabilityChanged. This event, as the name implies, allows you to check if network connectivity is available. You use the NetworkAvailableEventArgs IsNetworkAvailable read-only boolean property to do this. In practice, this event is somewhat limited because it only detects basic connectivity. For example, if your computer is plugged into a hub but the connection beyond the hub goes down, this event won’t fire. Therefore, in most cases, you would be better off using other methods to detect the network connectivity your application needs.

If you found this article helpful (or not) or if you have any questions about it, please feel free to leave me a comment.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB.NET Tutorials

Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (1 votes, average: 4 out of 5)
Loading ... Loading ...

Leave a Comment


Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed

Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles


Most Recent Articles


 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities