Solved

Visual Basic .net - How to detect that 'X' (close form) was clicked?

Posted on 2011-03-03
3
799 Views
Last Modified: 2012-05-11
How to detect that 'X' (close form) was clicked with mouse?
0
Comment
Question by:keith1001
3 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
ID: 35028527
In form closing, look at the CloseReason parameter
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If e.CloseReason = CloseReason.UserClosing Then
            'User closed through X button or control menu or alt-f4
        End If

    End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 35028646
To expand on PaulHews comment, you can cancel the user's close request with e.Cancel = True.  For example:

If e.CloseReason = CloseReason.UserClosing Then
e.Cancel = True
End If
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35029316
UserClosing will also be triggered with this code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

Open in new window


Another way is to trap the SC_CLOSE message from WM_SYSCOMAND:
Public Class Form1

    Private ClosedViaControlBox As Boolean = False

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close() ' <-- won't trigger code in FormClosing
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Const SC_CLOSE As Integer = &HF060&
        Const WM_SYSCOMMAND As Integer = &H112

        Select Case m.Msg
            Case WM_SYSCOMMAND
                Select Case m.WParam.ToInt32
                    Case SC_CLOSE
                        ' Occurs when 'X' is clicked in Top Right of Window
                        ' or 'Close' is selected from Right Clicking on the TaskBar
                        Me.ClosedViaControlBox = True

                End Select

        End Select

        MyBase.WndProc(m)
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Me.ClosedViaControlBox Then
            MessageBox.Show("User Initiated Close")
        End If
    End Sub

End Class

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question