How do I program the x button?

How do I program the x button in the top right to do whatever I want?
Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
For VB.Net, it's similar...

In VB.Net 2005:

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Select Case e.CloseReason
            Case CloseReason.UserClosing
                If somecondition Then
                    e.Cancel = True
                End If

        End Select
    End Sub
You don't do that exactly, but you can do this.

Capture the Event (Unload) on that form, and choose to close it or not, or to do something else.
Try this:


Private Sub Form_Unload(Cancel As Integer)

    MsgBox ("This will appear when I click the x button !!! ")

    if (I_Want_To_Close_This_Window) Then
           'Close all files and handles

           Cancel = 0      'This means that this event will get passed on
           'Do something else

           Cancel = 1     'This means that this event will NOT get passed on
    End If

End Sub


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.