Dear all,

I understand the form closing event.

But I want to detect when my app. is closing. It needs to update a status into the database, and only when the app. is closing. I have multiple forms in my application and people can close a app with the X or with a nice button. Therefore if I use the form closing event, I need to write the code on every form, because a user can close the app from every form in this app if he wanted to. Then I need to detect if all forms are closed before updating the status.

This status in the databse may only be updated when the app is closing (exit), not when a particular form is leaving or closed.

This have to do with a login status. When you log in with a particular account, a status is set into the database. When logging out, the status is updated. This prevents that not 2 people can log in with the same account. After leaving (logging out) the status is updated and somebody else can use this account.

The best thing tot do that is to detect if a user exits the application.

Many thanx for your help!


Recommended Answers

All 3 Replies

You can put the common code in a module then just make a call to that code in the form closing handler for each form.

Assuming that you are using VB's "application framework" you have a couple of options. To ensure that the framework is enabled, go to the "Solution Explorer" and either double-left click on "My Project" or right-click on it and select open.

This will open the project's property setting page.


Make sure that the "Enable application framework is checked". No notice the "Shutdown mode" combobox. In this case it is set to "When startup form closes". This means exactly as is implied, the application shutsdown when the startup form is closed.

Now assuming that you are using the "Application.Exit" method (not the "End" statement) to exit the application either via clicking your close program button or in the "FormClosing" event handler, you can put your logout code in just the "Startup Form's closing event handler. No need to put this logic in all of the forms. By using "Application.Exit" versus "End", you allow all message loops to complete first. The "End" statement does very little cleanup and like slamming the door closed on the application.

Another option would be to using the application's "Shutdown" event handler. You access the code page for this event by clicking on the "View Application Events" button shown in the above picture.



Now you add you logout code under the shutdown event. The "My" namespace is meant to be your extentable and since all your forms would be closed at this point and you would probably need the login name, I added a property to this class that you can store that name in. You would set this property something like this in your login code:

My.Application.loginName = "Fred Flintstone"

Clear as mud yet? :)

commented: Excellent advice +12
commented: excellent, should be a tutorial +3

Many thanx!


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.