Results 1 to 16 of 16

Thread: Dispose form by Close button [Resolved]

  1. #1

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question Dispose form by Close button [Resolved]

    I would like to add a "confirm exit?" message box when I press the Close(X) button at the top right of my form.

    So I try to modify the following code found at the Region " Windows Form Designer generated code ":

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Dispose()
    End If
    End If
    MyBase.Dispose(disposing)
    End Sub

    But I failed. What I one is:
    When I press the Close(X) button, the form is still open, and a message box will pop up asking whether you are sure to exit?
    If Yes, the form will be dispose.
    If No, no action is taken, and the form is still remain there.

    How can I do so?

    Please guide. Thank you.
    Last edited by albertlse; Aug 21st, 2003 at 08:33 PM.

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    I wouldn't recommend overloading Dispose, rather I suggest you use the .Closing event:
    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemWindowsFormsFormClassClosingTopic.htm
    "The Closing event occurs as the form is being closed. When a form is closed, all resources created within the object are released and the form is disposed. If you cancel this event, the form remains opened. To cancel the closure of a form, set the Cancel property of the CancelEventArgs passed to your event handler to true."

  3. #3

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question

    I go thru the help file u mentioned, but i not sure whether can implement in my problem, n i dont know how

    I have a menu File -> Exit

    When I click File, then Exit, to terminate the program, it will show a confirmation dialog box.
    The code I implemented is as below:

    Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
    If MessageBox.Show("Are you sure to exit?", Me.Text, _
    MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
    'Exit this application
    Application.Exit()
    End If
    End Sub

    How can I make the Close(X) button on the top right corner of the program to also call the above function before terminating the program? So that I will also get the confirmation dialog box when I press the close(X) button?

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Is this what you mean ?
    Attached Files Attached Files

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    That works, although I don't understand why, I thought it should be something more like:
    Code:
        Private Sub Form1_Closing(ByVal sender As Object, _
        ByVal e As System.ComponentModel.CancelEventArgs) _
        Handles MyBase.Closing
            If frm2.ShowDialog(Me) = DialogResult.Yes Then
                e.Cancel = True
            End If
        End Sub
    Why does it work without the If? Actually the above does NOT work. Huh?

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    As your code explain ,this way it won't work .When you close the form by the upper X then you should first show the form and handle handle the closing event . But in your above code , you are checking if the form already shown and then handle the event (cancel it ) .

  7. #7
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    This is what I would have done:
    Code:
            If MsgBox("Exit?", MsgBoxStyle.OKCancel) = DialogResult.Cancel Then
                e.Cancel = True
            End If

  8. #8
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Aha I didn't look at the code for Form2:
    Code:
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.Dispose(True)
            Application.Exit()
        End Sub
    Now I see.

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    If MsgBox("Exit?", MsgBoxStyle.OKCancel) = DialogResult.Cancel Then
    e.Cancel = True
    End If
    This won't work either I guess unless you already had your form shown .

  10. #10
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    If your app has only one form, or if the app only has one form open at the time the above code is run, then it will work, although Application.Exit (your suggestion) is better if there is a chance multiple forms might be open.

  11. #11

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question

    Thank you for your example! It is exactly what I want.
    Now I have the idea. Thank you.

    I wrote my code like this:

    'Close form by clicking the Cancel button
    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
    'Dispose form
    disposeForm()
    End Sub

    'Close form by clicking Close button
    Private Sub frmEntry_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    e.Cancel = True

    'Dispose form
    disposeForm()
    End Sub

    'Dispose form
    Private Sub disposeForm()
    If MessageBox.Show("Are you sure to cancel??", Me.Text, _
    MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
    'Dispose form
    Me.Dispose()
    End If
    End Sub

    If I press the cmdCancel button, it will call the disposeForm(), and then followed by frmEntry_Closing(...)

    It means the confirmation dialog box will pop up twice when I press the cmdCancel button. How to make it once only?

    Can we call the frmEntry_Closing(...) explicitly? what will be the parameters? how to set?

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    That's right . What's wrong with the code I posted . It gives you more control when you build your own End Window .

  13. #13
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    As for parameters , you can pass this

    VB Code:
    1. frmEntry_Closing(sender , e)

  14. #14

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question

    So does it means that no matter how I close a form, it will always execute the form_Closing (.....) function?

    Can we avoid this?

  15. #15
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Exactly , you can change your code behaviour to what I've suggested if you like .

  16. #16

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163
    it's working fine now

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