Click here to Skip to main content
15,662,532 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi all,

I have a winforms and i want to control the event before the system is exit. Please see my code below. Maybe you can help me with some idea :)

Private Sub frmDashboard_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim vAns As String = String.Empty
        vAns = MessageBox.Show("You will be logout from the system. Do you wish to continue?" & vbCrLf & _
                             "Click YES to logout." & vbCrLf & _
                             "Click NO to logout and terminate this system." & vbCrLf & _
                             "Click CANCEL to stay on system", "Confirmation!", MessageBoxButtons.YesNoCancel)
        If vAns = "0" Then
             ' Logout and back to login form
        ElseIf vAns = "1" Then
             ' Logout and terminate application
        ElseIf vAns = "2" Then
             ' Stay on current form
        End If
    Catch ex As Exception

    End Try
End Sub

By right if user click yes or no system will be closing no matter what. But if i click cancel, it should not do anything but since it is under FormClosing events, the system will close and not stay in the current form. How to do it?

Imagine i have frmLogin and frmDashboard. This action is on frmDashboard


I just found this link[^]. Can it answer my question here?
Updated 11-Feb-14 16:02pm

1 solution

To cancel closing (that is, to let the form stay, not closing it), assign true to e.Cancel.

This is all clearly written in documentation; you should only pay more attentions while reading it.

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