So someone posed this question to me. How do you cancel the close event on the form. This usually occurs when the user clicks the “red x” button on the form.
Here is what you do:
Private Sub dialog_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim dlgRes As Windows.Forms.DialogResult
dlgRes = MessageBox.Show(“Are you sure you wish to exit?”, “Closing”, MessageBoxButtons.YesNo, MessageBoxIcon.Information)
If dlgRes = Windows.Forms.DialogResult.Yes Then
Me.DialogResult = Windows.Forms.DialogResult.Yes
e.Cancel = False
Else
e.Cancel = True
End If
End Sub
Notice this is for the FormClosing event. This event uses a special set of event arguments called the FormClosingEventArgs. There is a property you can set that will cancel the close process named cancel.
The reason I am posting this is that while this seems so very obvious, it is suprisingly difficult to find this information for a new programmer so hopefully this will get tagged more appropriately in Google’s search engine.
Like this:
Like Loading...