Solved

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

Posted on 2011-03-03
3
802 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

734 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