Do something upon closing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gangreen
    New Member
    • Feb 2008
    • 98

    Do something upon closing

    I'd like to do something when a user presses the X in the top right corner. How?

    I work in VB.net
    Thx
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Windows Forms application or web application?

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Originally posted by r035198x
      Windows Forms application or web application?
      I'm willing to bet this is a Windows forms question.

      Add an event handler for your form's FormClosing event. To do this: select the form, and in the properties window click the lightning bolt icon (events) and doubleclick the blank space to the right of FormClosing.

      Make sure to take into account e.CloseReason when you do this. This is what the sub would look like.
      Code:
      	Private Sub Form2_FormClosing(ByVal sender As System.Object,_
      ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
      		If e.CloseReason = CloseReason.TaskManagerClosing_
      	     Or e.CloseReason = CloseReason.WindowsShutDown Then
      			'If either of these are true, 
      			'you want your app to shut down immediately.
      			e.Cancel = False
      		Else
      			'Do what you need to do here.
      			MessageBox.Show("Can't Close")
      			'if you want the closing to be canceled, set true, else false.
      			e.Cancel = True
      		End If
      	End Sub

      Comment

      • Gangreen
        New Member
        • Feb 2008
        • 98

        #4
        ah sorry, it's a forms indeed.

        I'll try it out right now, thanx!

        Comment

        Working...