Solved

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

Posted on 2011-03-03
3
784 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now