[2008] Close Message Box?
Hi, I need to close a messagebox without closing the application.
Here is my code:
Code:
MessageBox.Show("You may have unsaved work. Are you sure you want to quit?", "Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If vbYes Then
Application.Exit()
Else
DialogResult = Windows.Forms.DialogResult.No
End If
If I click yes, the app quits, but this is also the unexpected result of clicking no.
If anyone can help me, it is greatly appreciated!
Louix.
Re: [2008] Close Message Box?
Code:
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If MessageBox.Show("¿Do you want to quit?", "Exit program", MessageBoxButtons.YesNo) = DialogResult.No Then
e.Cancel = True
End If
End Sub
Re: [2008] Close Message Box?
Put the code in your FormClosing Event, you also need to store the result of the message box in a variable:
Code:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo)
If (result = Windows.Forms.DialogResult.No) Then
e.Cancel = True
End If
End Sub
Re: [2008] Close Message Box?
Thank you very much for responding, but:
Code:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim result As DialogResult = MessageBox.Show("You may have unsaved work. Are you sure you want to quit?", "Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If (result = Windows.Forms.DialogResult.No) Then
e.Cancel = True
Else
Application.Exit()
End If
End Sub
When I click yes to exit, the message box comes up once more. Can you shine some light on this?
Re: [2008] Close Message Box?
Don't call application.exit, your form is already closing.
Re: [2008] Close Message Box?
I'm sorry I didn't mention this(my bad :P) but I want the whole app to quit not just the form.
Re: [2008] Close Message Box?
You could put that in the FormClosed event.
Re: [2008] Close Message Box?
But that would mean the whole app closes down when one form is closed.
Isn't there a way to detect when the app is closing and then stick the code in there?
Re: [2008] Close Message Box?
Quote:
Originally Posted by Louix
But that would mean the whole app closes down when one form is closed.
Isn't there a way to detect when the app is closing and then stick the code in there?
That is what you asked for, when the form closes to close the whole application.