How to handle a form close event in vb.net

This code runs after the form has been closed, when it's being disposed.
Depending on how you're showing the form, it might not get disposed at all.

You need to handle the FormClosing event and set e.Cancel to True if you want to cancel the close.


  Private Sub frmProgramma_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If MessageBox.Show("Are you sur to close this application?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
    Else
      e.Cancel = True
    End If
  End Sub

or that is how i use it everytime over and over...


Use FormClosing event. MSDN