Gaven

If I want to run specific code when the "X" is clicked as opposed to the form being closed by other means, how do I do that



Re: Visual Basic General Event when the "X" is clicked

Rick

this will do it:

Code Block

Public Class Form1

Dim bClosedByMenuOrButton As Boolean = False

Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click

bClosedByMenuOrButton = True

Me.Close()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

bClosedByMenuOrButton = True

Me.Close()

End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

If Not bClosedByMenuOrButton Then

'run your specific code

MsgBox("closed by X")

Else

MsgBox("closed by menu or button")

End If

End Sub

End Class