If you need to know the reason the form is closing then try this:
VB Code:
Public Enum QueryUnloadModes SystemShutdown TaskManager MenuClose CodeCall Unknown End Enum Public Function QueryUnloadMode() As QueryUnloadModes 'PURPOSE: This function provides the same functionality that the QueryUnloadMode 'parameter did when closing a form in VB6. It returns the reason the form is closing. 'It has not been tested when run outside of the Closing event but does work as 'expected when called from the Closing event of a form. 'SOURCE: Most of the code found here is a variant of the following two sources: ' [url]http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=42556[/url] ' [url]http://www.gotdotnet.com/community/messageboard/Thread.aspx?id=40651[/url] Dim st As New System.Diagnostics.StackTrace(True) If st.FrameCount >= 15 Then If st.GetFrame(15).GetMethod.Name <> "WmSysCommand" Then Return QueryUnloadModes.SystemShutdown End If End If Dim O As New System.Diagnostics.StackTrace(True) Dim F As System.Diagnostics.StackFrame F = O.GetFrame(8) Select Case F.GetMethod.Name.ToString Case "SendMessage" 'Closing because of call in code Return QueryUnloadModes.CodeCall Case "CallWindowProc" 'Closing because of system menu click Return QueryUnloadModes.MenuClose Case "DispatchMessageW" 'Closing because of Task Manager Return QueryUnloadModes.TaskManager Case Else 'Don't Know why I'm closing!!?? Return QueryUnloadModes.Unknown End Select End Function
Then just call it from the closing event of the form and it will return the enum value for the reason.
VB Code:
Dim ret As QueryUnloadModes = QueryUnloadMode() If ret <> QueryUnloadModes.SystemShutdown Then e.Cancel = True Me.Hide() End If
It can also be put in a dll and used from there.




Reply With Quote