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!

Recommended Answers

All 6 Replies

You need to catch the close event like:

 Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) _
      Handles Me.FormClosing
        If e.CloseReason = CloseReason.UserClosing Then
            e.Cancel = True
            MsgBox("CLosing")
            Me.Hide()
        End If
    End Sub
commented: Thank you very much. Please see my comment +2

did you try to change the closing method in you appplication settings?
you should use 'when last form closes' or something this way (sorry i don't know the english version, my Vstudio is in Italian)

commented: Can you please tell me more about that? +2

Excuse me everyone, I decided to do just as Minimalist said, but my solution didn't work in many forms. In fact, it seems it won't work as effectively in more than two forms.

There is still a problem; For example, the first form is login form, after success in login, form1 will open and then a user may want to open form2. Problem: Since every form has a FormClosing event set, even when a user will open another form say Form2, the code me.close,Form2.Show will run causing FormClosing event of the previous form (form1)to run and eventually log off the user and exit the application (while the user just wanted to open another form.

Also, when I'm in my 3rd form (say), If I hit the big X (close), the FormClosing event for that 3rd form will be fired and when the form is closed, the FormClosing events of all the other forms I had opened will also be fired.(In the case where I choose to hide these forms) Its so confusing to me.

I think I explain myself well; I find out that when I close my form/exit application in my 3rd/4t form (just an example), all the other previous forms will have their FormClosing events fired, one after another which is a mess.

I still don't know how to figure this out. The only thing I can think of now is to disable the close button (big X) in all the other forms so that the use will have to log off only by clicking on the button/menu I provide.

More help please?

Thank you gizida, but I have already done so. If I disable the form closing event, how then can I have it logg off a user when a user clicks the close X button without logging off from the database? That's what made me use the form closing event.
Thanks.

what database? i cannot find anything about databases in the previous posts...
I think it would be useful if you explain what kind of application it is... maybe you can zip the source and put it online? I would be glad to check it out and help you find a solution... i used a login system for my operating system emulation and i had no troubles with forms closing so maybe the same approach can be used but i would need to see your app.

Be a part of the DaniWeb community

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