Solved

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

Posted on 2011-03-03
3
807 Views
Last Modified: 2012-05-11
How to detect that 'X' (close form) was clicked with mouse?
0
Comment
Question by:keith1001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 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