Difference between revisions of "VB.net UnloadMode"

From HashVB
Jump to: navigation, search
(How to tell if a user closes a form with the X or through code in VB.net)
 
m (How to tell if a user closes a form with the X or through code in VB.net)
Line 6: Line 6:
  
  
Private Const WM_SYSCOMMAND As Integer = &H112
+
Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_CLOSE As Integer = &HF060
+
Private Const SC_CLOSE As Integer = &HF060
  
Protected Overrides Sub WndProc(ByRef m As Message)
+
Protected Overrides Sub WndProc(ByRef m As Message)
 
     If m.Msg = WM_SYSCOMMAND And m.WParam.ToInt32 = SC_CLOSE Then
 
     If m.Msg = WM_SYSCOMMAND And m.WParam.ToInt32 = SC_CLOSE Then
 
         'The user clicked the X button to close this form.  
 
         'The user clicked the X button to close this form.  
Line 16: Line 16:
 
     End If
 
     End If
 
     MyBase.WndProc(m)
 
     MyBase.WndProc(m)
End Sub
+
End Sub

Revision as of 21:38, 2 May 2006

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.
       Return
   End If
   MyBase.WndProc(m)
End Sub