שתף באמצעות


Handler for Red 'X' Click on Forms

Question

Tuesday, April 4, 2017 2:36 PM

Does anyone know of an easy way to handle the red 'X' (in top right corner) click event? I done a bit of research, but solutions I have found so far work for all 'user-induced' form closes (eg, including pressing a button that has been added to the form). Is there a way to handle only a window red 'x' click? Thanks. 

All replies (7)

Tuesday, April 4, 2017 2:46 PM | 1 vote

Look at Form Closing event, you can work with e parameter e.g.

e.Cancel = true or e,Cancel = false depending on what you want to do

Imports System.ComponentModel

Public Class Form1
    Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        If MessageBox.Show("Exit", "Question", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            ' they decided to exit so let them
        Else
            e.Cancel = True
        End If
    End Sub
End Class

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator


Tuesday, April 4, 2017 2:46 PM | 1 vote

A way is to test if WM_NCHITTEST = HTCLOSE in CloseReason.UserClosing


Tuesday, April 4, 2017 2:46 PM

I'm not sure what you mean by "handle", but the FormClosing Event will be fired when you click on this button. Below is an example:

Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs) _ 
     Handles Form1.FormClosing

    if e.CloseReason = CloseReason.UserClosing then
        e.Cancel = true
        Me.Hide()
    end if

End Sub

Paul ~~~~ Microsoft MVP (Visual Basic)


Tuesday, April 4, 2017 5:49 PM

 If you want to handle the case of the user clicking the red [X] only, then you can handle the WM_NCLBUTTONDOWN message in the WinProc of the form like below.  You can also cancel the form from closing by setting the result of the message to 0 and returning.

Public Class Form1
    Private Const WM_NCLBUTTONDOWN As Integer = &HA1
    Private Const HTCLOSE As Integer = 20

    Protected Overrides Sub WndProc(ByRef m As Message)

        If m.Msg = WM_NCLBUTTONDOWN AndAlso m.WParam.ToInt32 = HTCLOSE Then

            'the user clicked the red [X] to close the form, do something...

            'use these 2 lines if you want to stop the form from closing, otherwise remove them.
            m.Result = CType(0, IntPtr)
            Return
        End If

        MyBase.WndProc(m)
    End Sub
End Class

 

 If you want to handle the user clicking the [X] or choosing Close on any of the system menus,  then you can handle the WM_SYSCOMMAND message in the form`s WindProc as shown below.  Again,  you can cancel the form from closing by setting the result to 0 and returning.

Public Class Form1
    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 AndAlso m.WParam.ToInt32 = SC_CLOSE Then

            'the user clicked the red [X], or Close on the system menu to close the form, do something...

            'use these 2 lines if you want to stop the form from closing, otherwise remove them.
            m.Result = CType(0, IntPtr)
            Return
        End If

        MyBase.WndProc(m)
    End Sub
End Class

 

If you say it can`t be done then i`ll try it


Wednesday, April 5, 2017 6:33 AM

Hi M477H3W,

You may use the FormClosing() Event, the event FormClosing() is triggered any time when a form is to get closed, FormClosing occurs when the form received the message to be closed, and verify whether it has something to do before it is closed.

More detailed, please refer to FormClosing() Event.

Best Regards,

Cherry Bu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Wednesday, April 5, 2017 10:32 AM

Does anyone know of an easy way to handle the red 'X' (in top right corner) click event? I done a bit of research, but solutions I have found so far work for all 'user-induced' form closes (eg, including pressing a button that has been added to the form). Is there a way to handle only a window red 'x' click? Thanks. 

 Reading your question closely,  it appears that you do not want to handle the form closing if the user calls "Me.Close" from a button click event.  If you call "Me.Close" from a button click event, the CloseReason will still be UserClosing.  So,  using the FormClosing event will not work to catch just when the user clicks the red [X].

 Take a look back at my first post,  i give a method which will only catch the red [X] being clicked but,  it will not catch it if you call "Me.Close" from a button click event.  8)

If you say it can`t be done then i`ll try it


Wednesday, April 12, 2017 7:43 AM

Hi M477H3W,

Please remember to close your thread by marking helpful post as answer, it is beneficial to the other communities who have the same issue.

Thanks for your understanding.

Best Regards,

Cherry Bu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.