Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi friends!
First, I have read other similar threads but I haven't seen a complete match.

It is my VB.NET app. I want the user to be logged out when a user closes the form/application by clicking the X at the right-top (to prevent the user from appearing to be online while not).

Now, I created a sub for LoggOff, put it in a module and call it whenever I want it. So I went to properties and double-clicked"Form Closing" event to write the code for FormClosing. Inside FormClose I call the "LogOff" sub explained above, ofcourse, now when the user clicks the big close 'X' at the top, he/she is logged off.But there is a problem;

The first form to load is the login form after which another form, say "Welcome" will be opened and "login" will be closed (by welcome.open, me.close), now the problem is, whenever me.close is executed, all the code in the FormClosing event is also run, which means terminating the application.

Whenever I just want to close one form and open another one (I don't want to just hide forms) the code will log the user off and close the application. How can I differentiate the closing of a user hitting the big X and the closing of a programmer with me.close???

Many thanks for any input.
Frank!
Posted
Updated 31-Jul-14 3:32am
v2

Assuming you're using Windows Forms:
Look at the CloseReason property[^] of the FormClosingEventArgs class[^] passed to your event handler. It will return one of the CloseReason values[^], which you can use to determine why the form is being closed.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jul-14 9:37am    
It looks like you gave a correct recipe for a typical question but did not understand what OP is doing. I also almost put some similar answer. This is not what is going on. Please see my answer.
(As you can see, I also get anonymous down-votes, and, on a pretty regular basis, from someone of high reputation; I don't say it's should be this way, but I used to it. :-)
—SA
Alternatively look at the ApplicationExit[^] event
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jul-14 9:38am    
Good alternative idea, 5ed. But this is not all. The form cannot be closed at all, no matter of log-of is done or not.
Please see my answer.
—SA
I'm thinking you need to look at the Project Properties. On the Application tab (for most versions of Visual Studio) you should see some options like Assembly Name, Root namespace, Application Type, Icon, etc. There is also a property for Shutdown mode. If it's set to "When Startup form closes" then any time you close the Startup form (which is also a property set on this page) the entire application will end. You may want to change that to "When last form closes." This way as long as you have a form open (could still be hidden, though) and haven't called Application.Exit(), your app will continue to run.

You also asked about telling the difference between closing a form with a button or using the x. I typically do that by having a class level boolean. It's set to False in the Load event. If I have a button that closes the form I make sure to set the variable to True before closing...like this:
VB
Private Sub btnHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHome.Click
    boolCloseByButton = True
    Home.Show()
    Me.Close()
End Sub


Then in my Form Closing I check that variable. If it's set to True I know it's closing because of a button I coded. If it's set to false I know that the user must have hit the X to close the form. If I don't really want the form to close I can use the e.Cancel inside the FormClosing event. For example, here is some code that I use to prompt users, only when they close by using the X, to make sure they want to close the form:
VB
Private Sub Form1(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If Not boolCloseByButton Then
        If MessageBox.Show("Do you want to exit?", gblstrFormText, MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            e.Cancel = True
        End If
    End If
End Sub

If the user selects no, the e.Cancel = True will stop the form from closing.

Hope this helps.
 
Share this answer
 
Comments
savedlema 1-Aug-14 5:34am    
Kschuler,
Thank you very much for your input. I managed to do it from your help.

However, I was forced to do some modifications to your code. I'm posting the solution hoping it may also be useful to someone else.
Thank you so much.
First of all, you really need to review your UI design.

As to your problem, it's way too simple. Just make the log-out code conditional. The fact that you close the form to show "welcome" in the code of the same form (you write me.close, not MyAuthenticationForm.Close only simplifies it: the flag for condition can be private; you would just check-up this flag:
VB
me.performLogoff = false; ' some private Boolean flag
me.close
me.performLogoff = true;

And this me.performLogoff should be checked by the code performing log-off.

One problem is solved, but it would create another problem. If a form is closed, you won't be able to use it again. Any attempt to show it again will throw the exception which tells you the form is disposed.

So, the real solution will be: any form to be shown again should not be closed; hide it instead.

SA
 
Share this answer
 
Thank you all for your inputs. Based on the suggestion by
Quote:
Kschuler
, this is how I did it so far:

VB
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing

      If Not boolCloseByButton Then
          Dim result1 As DialogResult = MessageBox.Show("Do you want to close?", _
                                "On FormClosing event", _
                                MessageBoxButtons.YesNo)

          If result1 = Windows.Forms.DialogResult.No Then
              e.Cancel = True
              MsgBox("Form is NOT closing or exiting application")
          Else
              MsgBox("Logging Off Now")
              'LogOff() will be executed here
          End If
      End If

  End Sub


Quote:
boolCloseByButton
is declared as Boolean and set to "False" during the form's Load event.

So far its working fine for me, I'll continue to experiment with it as I add more forms. May be this solution will help someone else in the future.

Thank you very much everyone.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900