Link to home
Start Free TrialLog in
Avatar of running32
running32

asked on

Cancel Form closing

I though it was e.cancel to stop the closing of a form.  This form still closes. When I trace it it does hit e.cancel = true but then still closes.  Any ideas? Thank you

Private Sub frmrecordsmanagement_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If addnew = False Then
            Dim ans As Int32
            ans = MsgBox("Do you want to Add new without saving the record first?", MsgBoxStyle.YesNo)
            If ans <> 6 Then
                addnew = False
                e.Cancel = True

            End If
        End If
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of PockyMaster
PockyMaster
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Tomlinson
Try something more like...

Public Class frmrecordsmanagement

    Private addnew As Boolean = False

    Private Sub frmrecordsmanagement_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If addnew = False Then
            Dim answer As DialogResult
            answer = MessageBox.Show("Do you want to Add new without saving the record first?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If answer = Windows.Forms.DialogResult.No Then
                e.Cancel = True
            End If
        End If
    End Sub

End Class
Avatar of wtconway
wtconway

Have you tried setting e.Cancel outside the If block? just to make sure nothing weird is happening? If it does not close while the e.Cancel = True is outside of the IF block then it's most likely something to do with the if statement.

Also, why not just do this on your msgbox.

If MsgBox("Do you want...",MsgBoxStyle.YesNo) <> MsgBoxResult.Yes
   addnew = false
   e.cancel = true
Avatar of running32

ASKER

e.cancel works outside of the if statement but once I place it inside the if statement it does not work.
uhm, did you think that maybe addnew = true?
Did you try the examples above?...
Also don't use 6 for the constant.. use the build in constants.. you will make no mistakes that way
It seems to me that addnew is True when the method starts to execute. Idle_Mind's code should work. So should mine. And so should PockyMaster.

I would try either debug.writeline on your addnew variable onces the method starts (the first line) or do a msgbox of the value. Just to see if it's getting changed unexpectedly elsewhere.
I'm not following the logic here.

First, the question in the messagebox looks a bit odd.  Assuming that AddNew=True indicates that there is an unsaved new record, shouldn't it be more on the lines of "Do you want to Close the form without saving the record first?"

Then, if my assumption about what the question really means is right, and the answer is "Yes", it means the user does want to proceed with the close without saving the record first.  So e.Cancel should be False.  It is only if the answer is "No" that the user has changed his/her mind: s/he does want to save the record before closing the form: so it is in this case that e.Cancel should be True.

Idle Mind's code is based on a different understanding of the real life scenario, I think - which, given what the message box actually says, is an equally valid interpretation.

But, for my money, it might be worth settling what the real life scenario is before trying to get the right code for it.

Roger
Oops.  I should have written "Assuming that AddNew=False indicates that ...".  But I'm not sure about that, either.

Roger
Thank you I have it worked out now.