locked
form closing on vb.net

    Question

  • hi, 
        im doing a project and i want to have an alert when my user are closing the form thats says "Exit are you sure?" with two buttons "yes" and "cancel". what will be the code? 
        please someone help me with this.
    Saturday, July 26, 2008 7:00 AM

Answers

  • Hello

    Catch the Form Closing event, and cancel the close from there:

    Public Class Form1  
     
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing  
     
            Try  
                If MsgBox("Are you sure", MsgBoxStyle.YesNo Or MsgBoxStyle.Question, "Form closeing") = MsgBoxResult.No Then  
     
                    e.Cancel = True 
                End If  
     
            Catch ex As Exception  
                MsgBox(ex.Message)  
            End Try  
     
        End Sub 
    • Proposed as answer by Molossus Sunday, July 27, 2008 12:35 PM
    • Marked as answer by Xingwei Hu Wednesday, July 30, 2008 1:53 AM
    Saturday, July 26, 2008 8:29 PM
  • You might also want to consider the close reason while placing a message box in the FormClosing event.  There are many reasons that a form closes that you may not want to interfere with (i.e. Windows shutting down, TaskManager-initiated (not sure you could stop it anyhow), Mdi form parent closing).  Improper use of this event can lead to hanging apps when the system is trying to shut down.  The args in this event tell you the reson so, for example you might want to use the messagebox for only a couple of the reasons...

     
       Private Sub Form1_FormClosing(ByVal sender As ObjectByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing  
     
            Try 
                If e.CloseReason = CloseReason.FormOwnerClosing OrElse e.CloseReason = CloseReason.UserClosing Then 
                    If MsgBox("Are you sure", MsgBoxStyle.YesNo Or MsgBoxStyle.Question, "Form closeing") = MsgBoxResult.No Then 
                        e.Cancel = True 
                    End If 
                End If 
     
            Catch ex As Exception  
                MsgBox(ex.Message)  
            End Try 
     
        End Sub 
     

    I placed two conditions for the hell of it but in the majority of cases just the UserClosing option indicates that the user has either clicked the close button or set the dialog result of the form through an action.


    • Marked as answer by Xingwei Hu Wednesday, July 30, 2008 1:53 AM
    Sunday, July 27, 2008 3:27 PM

All replies

  • Hi, kangkungkernitz1081

    I hope this code helps you

    Private Sub CloseButton_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles CloseButton.Click 
            If MessageBox.Show("Are you sure you want to exit?""Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then 
                Me.Close() 
            End If 
        End Sub 

    nyekzz

    I'm a noob! please help me
    Saturday, July 26, 2008 1:30 PM
  • Hello

    Catch the Form Closing event, and cancel the close from there:

    Public Class Form1  
     
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing  
     
            Try  
                If MsgBox("Are you sure", MsgBoxStyle.YesNo Or MsgBoxStyle.Question, "Form closeing") = MsgBoxResult.No Then  
     
                    e.Cancel = True 
                End If  
     
            Catch ex As Exception  
                MsgBox(ex.Message)  
            End Try  
     
        End Sub 
    • Proposed as answer by Molossus Sunday, July 27, 2008 12:35 PM
    • Marked as answer by Xingwei Hu Wednesday, July 30, 2008 1:53 AM
    Saturday, July 26, 2008 8:29 PM
  • You might also want to consider the close reason while placing a message box in the FormClosing event.  There are many reasons that a form closes that you may not want to interfere with (i.e. Windows shutting down, TaskManager-initiated (not sure you could stop it anyhow), Mdi form parent closing).  Improper use of this event can lead to hanging apps when the system is trying to shut down.  The args in this event tell you the reson so, for example you might want to use the messagebox for only a couple of the reasons...

     
       Private Sub Form1_FormClosing(ByVal sender As ObjectByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing  
     
            Try 
                If e.CloseReason = CloseReason.FormOwnerClosing OrElse e.CloseReason = CloseReason.UserClosing Then 
                    If MsgBox("Are you sure", MsgBoxStyle.YesNo Or MsgBoxStyle.Question, "Form closeing") = MsgBoxResult.No Then 
                        e.Cancel = True 
                    End If 
                End If 
     
            Catch ex As Exception  
                MsgBox(ex.Message)  
            End Try 
     
        End Sub 
     

    I placed two conditions for the hell of it but in the majority of cases just the UserClosing option indicates that the user has either clicked the close button or set the dialog result of the form through an action.


    • Marked as answer by Xingwei Hu Wednesday, July 30, 2008 1:53 AM
    Sunday, July 27, 2008 3:27 PM