form close reason

Hi!

I would like my form to be hidden when the use clicks on the window close button (the X button on the top-right corner). The e.CloseReason does not provide a value to determine that this particular button is the reason that is closing the form. This was very simple in VB6:


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbFormControlMenu Then
        Cancel = True
    End If
End Sub

 


How is this possible in .NET Forms



Answer this question

form close reason

  • Sachin chakote

    Replace the '&' with an 'And'.



  • Choong

  • matt01

    Hi,

    On .NET winforms, you can try something like this:

    protected override void OnClosing(CancelEventArgs e)
    {
       if ( <insert your condition here> ) e.Cancel = true;
       else base.OnClosing (e);
    }


    The e.Cancel parameter from the received event data is a boolean value where you can set to true to cancel the event.

    -chris

  • alche

    Try the following:


    using System;
    using System.ComponentModel;
    using System.Security.Permissions;
    using System.Windows.Forms;

    namespace MyApplication
    {
        public class Form1 : Form
        {
            private const int WM_SYSCOMMAND = 0x0112;
            private const int SC_CLOSE = 0xF060;

            private bool _UserClose;

            public Form1()
            {
            }

            protected override void OnClosing(CancelEventArgs e)
            {
                if (_UserClose)
                {
                    MessageBox.Show("The user clicked the X button.");
                }

                base.OnClosing(e);
            }

            [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
            protected override void WndProc(ref Message m)
            {
                if (m.Msg == WM_SYSCOMMAND && ((int)m.WParam & 65520) == SC_CLOSE)
                {
                    _UserClose = true;
                }

                base.WndProc(ref m);

                _UserClose = false;
            }
        }
    }


     



  • Matt Masson

    Hi,

    Take a look at the following code sample:
    http://dotnet.mvps.org/dotnet/samples/windowsandforms/CloseWindow.zip

    A simpler option using Form1_Closing Event would be to use the following: I am not sure how to detect the other options but you can find out by trial and error with this approach: * Not sure if this works - still verifying*

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            
            Dim objStackFrame As StackFrame = New StackTrace(True).GetFrame(7)
            If objStackFrame.GetMethod().Name = "CallWindowProc" Then
                e.Cancel = True
            End If

    End Sub

    Regards,
    Vikram

  • Billy B

    Thanks both!
    I tried to use your code David in VB.
    Is the equivelant of the two constants declaration the following

    Private Const WM_SYSCOMMAND As Integer = &H112
        Private Const SC_CLOSE As Integer = &HF060

     

    If so, your code works only if I make the following modification:
    (int)m.WParam == SC_CLOSE)
    instead of
    ((int)m.WParam & 65520) == SC_CLOSE)



  • thomastwo

    Yes, I know the cancel property. What I'm asking is how my code will know if the form is closing because the end-user pressed the close button and not for any other reason.


  • Jeff Peil

    thanks David. That was it!


  • form close reason