Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle a form close event in vb.net

I have used the below code but its not showing the msgbox. What is wrong with this code ?

Private Sub frmSimple_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
       Dim result = MsgBox("Are you sure you want to Exit ?", vbYesNo)
       If result = DialogResult.Yes Then
        me.Close()
       End If
End Sub
like image 978
Failed_Noob Avatar asked Apr 27 '11 14:04

Failed_Noob


3 Answers

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.

like image 177
SLaks Avatar answered Oct 20 '22 07:10

SLaks


  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...

like image 32
ProblemAnswerQue Avatar answered Oct 20 '22 06:10

ProblemAnswerQue


Use FormClosing event. MSDN

like image 3
Jack Avatar answered Oct 20 '22 05:10

Jack