locked
The X closing button

    Question

  • Hello people.

     

    I need some help with this problem. When a user closes a program via the red X in most programs it asks them if they would like to save their work and so on (in MS word anyway) How can I do this for my program. What is the X button even called?

     

    Thanks

     

    P.S Why don't I get any alerts even though I checked send me alerts, happens in Microsoft Answers, Technet and here........any mod/admin know?

    Friday, September 24, 2010 7:27 PM

Answers

  • Look in your form events. it is called 'FormClosing'

    Tom
    • Marked as answer by Asim09 Friday, September 24, 2010 9:11 PM
    Friday, September 24, 2010 7:42 PM
  • as Tom said, use the FormClosing event

    there is a CloseReason property that you can test if the user is closing the app. Something like this:

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If e.CloseReason = CloseReason.UserClosing Then
          If MessageBox.Show("Save your work?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            'other code here to save
            MessageBox.Show("Saved")
          Else
            e.Cancel = True
          End If
    
        End If
      End Sub
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
      End Sub
    
    • Marked as answer by Asim09 Friday, September 24, 2010 9:11 PM
    Friday, September 24, 2010 7:52 PM

All replies

  • Look in your form events. it is called 'FormClosing'

    Tom
    • Marked as answer by Asim09 Friday, September 24, 2010 9:11 PM
    Friday, September 24, 2010 7:42 PM
  • as Tom said, use the FormClosing event

    there is a CloseReason property that you can test if the user is closing the app. Something like this:

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If e.CloseReason = CloseReason.UserClosing Then
          If MessageBox.Show("Save your work?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            'other code here to save
            MessageBox.Show("Saved")
          Else
            e.Cancel = True
          End If
    
        End If
      End Sub
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
      End Sub
    
    • Marked as answer by Asim09 Friday, September 24, 2010 9:11 PM
    Friday, September 24, 2010 7:52 PM