|
-
Jul 20th, 2008, 12:17 PM
#1
Thread Starter
Addicted Member
[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.
-
Jul 20th, 2008, 12:22 PM
#2
New Member
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
-
Jul 20th, 2008, 12:22 PM
#3
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
-
Jul 20th, 2008, 12:35 PM
#4
Thread Starter
Addicted Member
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?
-
Jul 20th, 2008, 12:41 PM
#5
Re: [2008] Close Message Box?
Don't call application.exit, your form is already closing.
-
Jul 20th, 2008, 12:45 PM
#6
Thread Starter
Addicted Member
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.
-
Jul 20th, 2008, 12:46 PM
#7
Re: [2008] Close Message Box?
You could put that in the FormClosed event.
-
Jul 20th, 2008, 12:48 PM
#8
Thread Starter
Addicted Member
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?
-
Jul 20th, 2008, 12:55 PM
#9
Re: [2008] Close Message Box?
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|