How to handle form closing event in vb.net

I used the code below but did not show 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 
+6
source share
6 answers

This code runs after the form was closed when it was deleted.
Depending on how you show the form, it cannot be deleted at all.

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

+14
source
  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 so I use it every time again and again ...

+6
source

Use the FormClosing event. MSDN

+3
source
  Dim result = MsgBox("Are you sure you want to Exit ?", vbYesNo) If result = vbYes Then me.Close() End If 
+3
source

I think it is more clean and simple!

 If MsgBox("Are you sure you want to Exit ?", vbYesNo) = vbNo Then e.Cancel = True 
+1
source
 If MessageBox.Show("¿Exit?", "Application, MessageBoxButtons.YesNo, _ MessageBoxIcon.Question) = DialogResult.No Then e.Cancel = True End If 
+1
source

Source: https://habr.com/ru/post/886803/


All Articles