Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Cancel Form closing

Posted on 2006-04-28
11
Medium Priority
?
1,242 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:running32
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 14

Accepted Solution

by:
PockyMaster earned 2000 total points
ID: 16564109
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If (MessageBox.Show("Are you sure?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No) Then
            e.Cancel = True
        End If


    End Sub

this should work ( in vs2005)

Copy the construction in your code (in your event handler) should work..
I don't understand though , why you are not using the dialogresult.no but use a constant 6 and also
you check if addnew = false then addnew = false... makes no sense to me :D

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16564120
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
0
 
LVL 7

Expert Comment

by:wtconway
ID: 16564133
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:running32
ID: 16564222
e.cancel works outside of the if statement but once I place it inside the if statement it does not work.
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16564231
uhm, did you think that maybe addnew = true?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16564235
Did you try the examples above?...
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16564239
Also don't use 6 for the constant.. use the build in constants.. you will make no mistakes that way
0
 
LVL 7

Expert Comment

by:wtconway
ID: 16564278
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.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 16564625
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
0
 
LVL 34

Expert Comment

by:Sancler
ID: 16564650
Oops.  I should have written "Assuming that AddNew=False indicates that ...".  But I'm not sure about that, either.

Roger
0
 

Author Comment

by:running32
ID: 16598237
Thank you I have it worked out now.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question