Results 1 to 9 of 9

Thread: [2008] Close Message Box?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

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

  2. #2
    New Member
    Join Date
    Jul 2008
    Posts
    8

    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

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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?

  5. #5
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] Close Message Box?

    Don't call application.exit, your form is already closing.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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.

  7. #7
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] Close Message Box?

    You could put that in the FormClosed event.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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?

  9. #9
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width