VB.NET - form closing code is not right - Asked By jenn on 22-May-10 01:59 PM

hi guys :)

I am still working on this notepad and I am now having trouble with my closing. The closing code is not picking up the fact that the save button was used. In otherwords...I can save my changes, and then hit the X to close the form and the dialog will ask me if I want to exit without saving changes.

here is the closing code that I am using:
Private Sub frmMyNotes_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MessageBox.Show("Are you sure you want to exit without saving changes?", "Exiting", _
            MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
            e.Cancel = True
        End If
    End Sub
not sure what I need to do here at all to fix this. I just know it is not complete...
Phivos Stylianides replied to jenn on 22-May-10 02:15 PM
Your method is correct. It won't magically know that you hit the save button! Here is what you can do. Keep a boolean variable in your class called isChanged. Whenever the user makes any changes to the text set it to true and whenever the user clicks save set it to false. Then in your FormClosing method check if that value is true and only then display the dialog box, otherwise continue with closing the form.
Anoop S replied to jenn on 22-May-10 02:25 PM
use this code
Private Sub form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

e.CloseReason = CloseReason.UserClosing Then
 Dim answer As Object
answer = MessageBox.Show("Are you sure you want to Exit the Application", "Exit",MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
 If answer = MsgBoxResult.Cancel Then e.Cancel = True
        Else
        Application.Exit()
        End If
    End Sub
jenn replied to Phivos Stylianides on 22-May-10 02:59 PM
good morning Phivos :)

yes I have come across the boolean variable on another form I am making and you are correct..on that form I do not have this issue!

so thank you for pointing me back to this method!

jenn replied to Anoop S on 22-May-10 03:06 PM
this method is giving me several errors :(

here is the code:
Private Sub frmMyNotes_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.CloseReason = CloseReason.UserClosing Then
        Dim answer As Object
        answer = MessageBox.Show("Are you sure you want to Exit the Application", "Exit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
        If answer = MsgBoxResult.Cancel Then e.Cancel = True
        Else
        Application.Exit()
        End If
    End Sub
I am being told that Close Reason is not a member, Else must have matching ElseIf, end must have matching endIf, a a end of expression is expected..
Phivos Stylianides replied to jenn on 22-May-10 03:48 PM
Morning Jenn!

The method you pasted here, when the form closes it directly displays the dialog box without checking any condition first. So if you want to check that flag and then if true show the dialog it should be something like this:

Private Sub frmMyNotes_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If isChanged = True Then
       If MessageBox.Show("Are you sure you want to exit without saving changes?", "Exiting", _
             MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
             e.Cancel = True
         End If
End If
    End Sub
jenn replied to Phivos Stylianides on 22-May-10 05:03 PM
Okay, now what am I doing wrong cuz I dont get a dialog box at all now LOL...hit close and it closes! heheheheee O God.
Dim isChanged As Boolean
Private Sub frmMyNotes_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If isChanged = True Then
                If MessageBox.Show("Are you sure you want to exit without saving changes?", "Exiting", _
                     MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
                    e.Cancel = True
                End If
            End If
            End Sub
Phivos Stylianides replied to jenn on 22-May-10 06:26 PM
Are you doing anything with the isChanged variable i wrote? Because if you don't set it to true when the user makes changes to the text it will stay false and the dialog box won't show.
Goniey N (Mr. G) replied to jenn on 23-May-10 12:31 AM
Use This Code :


Private Sub frmMyNotes_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MessageBox.Show("Are you sure you want to exit without saving changes?", "Exiting", _
            MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
            Me.close()    
    Or Name Of Form Here Both Will Work
    frmMyNotes.Close()
        End If
    End Sub
jenn replied to Phivos Stylianides on 23-May-10 10:12 AM
ahaaaaaaaaaa! you were right, I was not telling IsChanged what to do with itself....LOL and stickin it where the sun dont shine was being rejected as a option!

here is the one that worked :)..thank you so much again for your help!
Private IsChanged As Boolean = True
    Private Sub frmMyNotes_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If IsChanged = True Then
            If MessageBox.Show("Are you sure you want to exit without saving changes?", "Exiting", _
                 MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
                e.Cancel = True
            End If
        End If
    End Sub
Phivos Stylianides replied to jenn on 23-May-10 10:14 AM
You're welcome!
jenn replied to Goniey N (Mr. G) on 23-May-10 10:15 AM
thank you for your post I did try this method but when I click no..the dialog box stays and will only disappear when I click yes..this quite possibly could be an error on my part, but I did go with Phivos on his recommendations and have it working perfectly now :)
Super Man replied to jenn on 24-May-10 12:47 AM

I assumed that you have taken RichTextBox.

there is one Property Modified, you can make use of it, and you can also set its value.

whenever you right in RichTextBox. Modified property returns true. so whenever user formclose then use 

this condition to check whether its modified or not.

for save button: add  one more  statement:

RichTextBox1.Modified = False






 and in form_closing

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

 

        If (RichTextBox1.Modified) Then


            If MessageBox.Show("Are you sure you want to exit without saving changes?", "Exiting", _

            MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then

                e.Cancel = True

            End If


        End If

       

 

    End Sub