vb.netformclosing

Close Application with FormClosing Event VB.net


I try to exit my application with form closing event but confirmation message box appears twice.

This is what I have:

Private Sub FrmMainPlatform_FormClosing(sender As Object, e As FormClosingEventArgs) _
 Handles MyClass.FormClosing

    Dim result As Integer
    result = MessageBox.Show("Are you want to close", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.None)
    If (result = DialogResult.No) Then
        e.Cancel = True
    Else
        Application.Exit()
    End If

End Sub

I also tried with this solution:

Private Sub FrmMainPlatform_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select Case MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    Case Windows.Forms.DialogResult.Yes
        'nothing to do here the form is already closing
    Case Windows.Forms.DialogResult.No
        e.Cancel = True 'cancel the form closing event
        'minimize to tray/hide etc here 
    End Select
End Sub

The form is closed but the application i still running.


Solution

  • Private Sub FrmMainPlatform_FormClosing(sender As Object, e As FormClosingEventArgs) _
        Handles Me.Closing
    
        Dim result As Integer
        result = MessageBox.Show("Are you want to close", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.None)
        If (result = DialogResult.No) Then
            e.Cancel = True
        Else
            Application.Exit()
        End If
    
    End Sub
    

    Hi, I got temporary solution for this issue. The Exit method does not raise the Closed and Closing events, which are obsolete as of .NET Framework 2.0