VB.NET: Abort FormClosing()

让人想犯罪 __ 提交于 2019-12-01 03:52:08

You could try something like

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    If (MessageBox.Show("Close?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No) Then
        e.Cancel = True
    End If
End Sub

Have a look at

FormClosingEventArgs Class

And

CancelEventArgs.Cancel Property

The event can be canceled by setting the Cancel property to true.

'Button2 and closebutton of the form both closes the form asking the same 'question

Dim button2Yes As Boolean = False Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    If MessageBox.Show("    Sure to close?   ", "CLOSING CONTROL", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
        button2Yes = True
        Me.Close()
    Else
        button2Yes = False
    End If
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.FormClosing
    If Not button2Yes Then
        If Not MessageBox.Show("    Sure to close?   ", "CLOSING CONTROL", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            e.Cancel = True
        End If
    End If
End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!