Windows Forms application or web application?
I'm willing to bet this is a Windows forms question.
Add an event handler for your form's FormClosing event. To do this: select the form, and in the properties window click the lightning bolt icon (events) and doubleclick the blank space to the right of FormClosing.
Make sure to take into account e.CloseReason when you do this. This is what the sub would look like.
-
Private Sub Form2_FormClosing(ByVal sender As System.Object,_
-
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
-
If e.CloseReason = CloseReason.TaskManagerClosing_
-
Or e.CloseReason = CloseReason.WindowsShutDown Then
-
'If either of these are true,
-
'you want your app to shut down immediately.
-
e.Cancel = False
-
Else
-
'Do what you need to do here.
-
MessageBox.Show("Can't Close")
-
'if you want the closing to be canceled, set true, else false.
-
e.Cancel = True
-
End If
-
End Sub