Results 1 to 19 of 19

Thread: Managing the Close button [Resolved]

  1. #1

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question Managing the Close button [Resolved]

    Can we manage the Close (X) button found at the top right corner of every form?

    If I would like a display a dialog box when the Close button is pressed, how can I do so?

    Please help. Thank you!
    Last edited by albertlse; Aug 21st, 2003 at 08:36 PM.

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    VB Code:
    1. Private Sub myForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.         MessageBox.Show("Close")
    3.     End Sub

    This shows a message box when the form is closing.

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    This shows a message box when the form is closing.
    But not essentially when X button clicked.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you need to know the reason the form is closing then try this:
    VB Code:
    1. Public Enum QueryUnloadModes
    2.         SystemShutdown
    3.         TaskManager
    4.         MenuClose
    5.         CodeCall
    6.         Unknown
    7.     End Enum
    8.  
    9.     Public Function QueryUnloadMode() As QueryUnloadModes
    10.         'PURPOSE: This function provides the same functionality that the QueryUnloadMode
    11.         'parameter did when closing a form in VB6.  It returns the reason the form is closing.
    12.         'It has not been tested when run outside of the Closing event but does work as
    13.         'expected when called from the Closing event of a form.
    14.         'SOURCE: Most of the code found here is a variant of the following two sources:
    15.         '   [url]http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=42556[/url]
    16.         '   [url]http://www.gotdotnet.com/community/messageboard/Thread.aspx?id=40651[/url]
    17.         Dim st As New System.Diagnostics.StackTrace(True)
    18.  
    19.         If st.FrameCount >= 15 Then
    20.  
    21.             If st.GetFrame(15).GetMethod.Name <> "WmSysCommand" Then
    22.                 Return QueryUnloadModes.SystemShutdown
    23.             End If
    24.         End If
    25.  
    26.         Dim O As New System.Diagnostics.StackTrace(True)
    27.         Dim F As System.Diagnostics.StackFrame
    28.  
    29.         F = O.GetFrame(8)
    30.  
    31.         Select Case F.GetMethod.Name.ToString
    32.             Case "SendMessage"
    33.                 'Closing because of call in code
    34.                 Return QueryUnloadModes.CodeCall
    35.             Case "CallWindowProc"
    36.                 'Closing because of system menu click
    37.                 Return QueryUnloadModes.MenuClose
    38.             Case "DispatchMessageW"
    39.                 'Closing because of Task Manager
    40.                 Return QueryUnloadModes.TaskManager
    41.             Case Else
    42.                 'Don't Know why I'm closing!!??
    43.                 Return QueryUnloadModes.Unknown
    44.         End Select
    45.  
    46.     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:
    1. Dim ret As QueryUnloadModes = QueryUnloadMode()
    2.  
    3.         If ret <> QueryUnloadModes.SystemShutdown Then
    4.             e.Cancel = True
    5.             Me.Hide()
    6.         End If

    It can also be put in a dll and used from there.

  5. #5

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question

    Edneeis,

    I appreciate your reply.

    But understand nothing of what you had explained.

    Can you please explain to me in more details?

    Thank you.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well if you programmed in VB6 you'd be familiar with UnloadMode which was an argument that was in the QueryUnload event of a form which told you why the form was closing (i.e because of code, system shut down, close button..). In .NET there is no suck argument so this provides basically the samething. I'm not sure its what you were looking for but I had just read your post and used the class and thought someone might find it useful as well.

    If all you need to do is capture when a form is closing then you don't need it, you can just use the Closing event as previously mentioned. This code is intended to be used in the Closing event IF you need to know WHY the form is closing not WHEN.

  7. #7
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    It seems to me that QueryUnloadMode doesn't trap for specifically when the X button is pushed?

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Do you mean in VB6?

  9. #9
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    I think the original poster was asking about .NET (I'm curious too).

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I think I was confused for a second you were asking about the QueryUnloadMode code I posted right? I thought you were asking about QueryUnload in VB6.

    It should trap for the X close button and return MenuClose. I haven't tested on multiple OSs but it worked on Windows 2000 and XP.

    Just run this in the closing event:
    VB Code:
    1. Msgbox(QueryUnloadMode.ToString)

  11. #11
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Yeah that's what I thought it did - but MenuClose is also returned from Menu -> Close (presumably Form.Close)?

  12. #12

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question

    I mean in VB .NET.

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yes the code posted is for VB.NET sorry about the confusing. I brought up VB6 because it mirrors something already available in VB6.

    Slow_Learner, I don't understand your question, what else would Menu->Close return?

    SystemShutdown - Returned when the request to close the application or form is caused by Windows shutting down.

    TaskManager - Returned when the application process is closed via the TaskManager.

    MenuClose - Returned when the close button of the form, the little X in the corner, causes the form or application to close.

    CodeCall - Returned when the form or application is closed due to a request from code (i.e. Me.Close).

    Unknown - Returned when it was not able to determine the reason the form or application is closing.

    There may be trouble with it returning SystemShutdown instead of Code on XP machines. I'm looking into this.

  14. #14
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    //Slow_Learner, I don't understand your question, what else would Menu->Close return?

    This is kind of a tangent from what the original poster wanted to know - what I was curious about was how you could discriminate between a form closing by pressing the X button, and any other method. Guess you'd use Form.MouseDown and look for where the top right corner of the form is. Never mind, I was over-curious

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Fixed the trouble with Shutdown instead of CodeCall:
    VB Code:
    1. Public Enum QueryUnloadModes
    2.         SystemShutdown
    3.         TaskManager
    4.         MenuClose
    5.         CodeCall
    6.         Unknown
    7.     End Enum
    8.  
    9.     Public Function QueryUnloadMode() As QueryUnloadModes
    10.         'PURPOSE: This function provides the same functionality that the QueryUnloadMode
    11.         'parameter did when closing a form in VB6.  It returns the reason the form is closing.
    12.         'It has not been tested when run outside of the Closing event but does work as
    13.         'expected when called from the Closing event of a form.
    14.         'SOURCE: Most of the code found here is a variant of the following two sources:
    15.         '   [url]http://www.gotdotnet.com/Community/...d.aspx?id=42556[/url]
    16.         '   [url]http://www.gotdotnet.com/community/...d.aspx?id=40651[/url]
    17.         Dim st As New StackTrace(True)
    18.  
    19.         Dim O As New System.Diagnostics.StackTrace(True)
    20.         Dim F As System.Diagnostics.StackFrame
    21.  
    22.         F = O.GetFrame(8)
    23.  
    24.         Select Case F.GetMethod.Name.ToString
    25.             Case "SendMessage"
    26.                 'Closing because of call in code
    27.                 Return QueryUnloadModes.CodeCall
    28.             Case "CallWindowProc"
    29.                 Try
    30.                     If st.GetFrame(15).GetMethod.Name = "WmSysCommand" Then
    31.                         'Closing because of system menu click
    32.                         Return QueryUnloadModes.MenuClose
    33.                     Else
    34.                         'Closing because of system shut down
    35.                         Return QueryUnloadModes.SystemShutdown
    36.                     End If
    37.                 Catch
    38.                     'Don't Know why I'm closing!!??
    39.                     Return QueryUnloadModes.Unknown
    40.                 End Try
    41.             Case "DispatchMessageW"
    42.                 'Closing because of Task Manager
    43.                 Return QueryUnloadModes.TaskManager
    44.             Case Else
    45.                 'Don't Know why I'm closing!!??
    46.                 Return QueryUnloadModes.Unknown
    47.         End Select
    48.  
    49.     End Function

  16. #16
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Maybe the phrase MenuClose is not appropriate since it isn't from a MainMenu or MenuItem. MenuClose is returned from the X of the from otherwise something else is returned. Slow_Learner this code does exactly what you are talking about. Here try this example to understand. It is a tangent from the original posters request, but I posted it anyway.
    Attached Files Attached Files

  17. #17
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    I meant the control menu in the top left (alt + space menu), which does seem to return the same thing as the X button in the top right. Thanks for the example.

  18. #18
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yes I believe that is the context menu of the buttons in the corner so I'm afraid it does the samething as pressing the x, sorry.

  19. #19
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    No problem at all, I was just over-curious

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width