Member Avatar for iamthwee

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...

Member Avatar for iamthwee

Oooops I spelt the title wrong.

Member Avatar for iamthwee

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

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
                Me.Dispose()
                Me.Close()
            Else
                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! :|

commented: Excellent +11
Member Avatar for iamthwee

Thanx that actually did the trick.

Member Avatar for iamthwee

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
        Else
            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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.