VB.net UnloadMode

From HashVB
Jump to: navigation, search
 This article is based on Visual Basic.NET. Find other Visual Basic.NET articles.

Using VB.net, you may be wondering.. What happened to UnloadMode? How can I tell if a form was closed by clicking X or closed through code? I'm suprised that we don't see this question a hundred times a day in #VB, seeing as it's so often needed..

With VB.net, we have to override the WndProc event, checking for the appropriate system message.. They apparently didn't see it necessary to give us information in the form_closing event on why it's closing..

Anyway, add the following to your form, and adjust as needed:

Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_CLOSE As Integer = &HF060
Protected Overrides Sub WndProc(ByRef m As Message)
   If m.Msg = WM_SYSCOMMAND And m.WParam.ToInt32 = SC_CLOSE Then
       'The user clicked the X button to close this form. 
       'Do whatever you want here, of course.
   End If
End Sub