Ok in the application I have, I want to display a little msg "do you want to save" when the users presses 'x'.

In other words the user cannot close the window without first choosing one of the two buttons in the msg box.

How do I do this. Gimme code please...

11 Years
Discussion Span
Last Post by arjunsasidharan

you know how you get events like form1_load which you can then put code under? you can make one for form1_terminate by using the combo boxes at the top of the code editor, they list all the controls and possible methods.


It should be user presses 'x'. But I don't know how to edit the title?

Dim mblnexit As Boolean'Declare a boolan
public ApplicationName As String = "Howdy"
Private Sub Form_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Dim lstrMsg As String
        If mblnexit = False Then
            lstrMsg = MsgBox("Are You sure to Save the Application", MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, ApplicationName)
            If lstrMsg = Microsoft.VisualBasic.vbYes Then
                e.Cancel = True
            End If
      End If
End Sub

iamthewee.. If this is what you want then use it. If its not then don't give me darn bad rep! :|

Votes + Comments

I just tried arjunsasidharan's code and I keep getting error, object not set to reference.

After some hunting I found the following which appears more reliable.

Private Sub Form_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If MessageBox.Show("Are you sure to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            e.Cancel = False
            e.Cancel = True
        End If
    End Sub

The error you were getting should have been because of the Application name supplied to the message box.. declare it on a class file and it should work fine. Anyways your code will work.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.