Hello again guys, this time I need help with the FormClosing() Event in the Picture Viewing application I made. I wanted it to prompt the user before closing the form (if he had any unsaved changes) and I did it without help. But, the bad thing is, when someone saves a picture, the prompt will appear again! The code that I used in order to get this half-working is below. BTW, thanks in advance.

Private Sub ChildForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If picShowPicture.Image IsNot Nothing Then
            If MessageBox.Show("You've got unsaved changes. Are you sure you wish to proceed in closing this file without saving?", "Gavo® Ultimate Suite 2010", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
                e.Cancel = True
            End If
        End If
    End Sub

Recommended Answers

All 7 Replies

Add a member to the class to indicate it has been saved:

Public Class frmSave
	Dim saved As Boolean = False

	Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
		saved = True
		Me.Close()
	End Sub

	Private Sub ButtonCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCancel.Click
		Me.Close()
	End Sub

	Private Sub frmSave_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
		If (saved = False) And (picShowPicture.Image IsNot Nothing) Then
			If MessageBox.Show("You've got unsaved changes. Are you sure you wish to proceed in closing this file without saving?", "Gavo® Ultimate Suite 2010", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
				e.Cancel = True
			End If
		End If
	End Sub

End Class

Thank you man, but I don't quite understand what to do. Can you be more specific? Sorry, I don't know very much about VB.NET.

Here's my whole form's code. Please tell me what to change:

Public Class ChildForm

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Try
            If ofdSelectPicture.ShowDialog = DialogResult.OK Then
                picShowPicture.Image = Image.FromFile(ofdSelectPicture.FileName)
                Me.Text = "Image Viewer(" & ofdSelectPicture.FileName & ")"
                lblStatus.Text = "An image is loaded"
            End If
        Catch objException As System.OutOfMemoryException
            MessageBox.Show("The selected file is not supported by this program.", "Unsupported File", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        End Try
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Dim img As Image = picShowPicture.Image
        img = picShowPicture.Image
        picShowPicture.Image = Nothing
        If Not (img Is Nothing) Then
            img.Dispose()
            Me.Text = ""
            lblStatus.Text = "No image is loaded"
        End If
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        If (picShowPicture.Image Is Nothing) Then
            MessageBox.Show("Please choose an image first!", "Nothing to save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        If (ofdSavePicture.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            If (System.IO.File.Exists(ofdSavePicture.FileName)) Then
                System.IO.File.Delete(ofdSavePicture.FileName)
            End If
            picShowPicture.Image.Save(ofdSavePicture.FileName)
            lblStatus.Text = "An image is saved"
            Me.Text = "Image Viewer - Save Path(" & ofdSavePicture.FileName & ")"
        End If
    End Sub

    Private Sub picShowPicture_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles picShowPicture.MouseLeave
        lblX.Text = ""
        lblY.Text = ""
    End Sub

    Private Sub picShowPicture_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picShowPicture.MouseMove
        lblX.Text = "X: " & e.X
        lblY.Text = "Y: " & e.Y
    End Sub

    Private Sub ChildForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If picShowPicture.Image IsNot Nothing Then
            If MessageBox.Show("You've got unsaved changes. Are you sure you wish to proceed in closing this file without saving?", "Gavo® Ultimate Suite 2010", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub ChildForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblX.Text = ""
        lblY.Text = ""
    End Sub

    Private Sub OpenPictureToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenPictureToolStripMenuItem.Click
        Try
            If ofdSelectPicture.ShowDialog = DialogResult.OK Then
                picShowPicture.Image = Image.FromFile(ofdSelectPicture.FileName)
                Me.Text = "Image Viewer(" & ofdSelectPicture.FileName & ")"
                lblStatus.Text = "An image is loaded"
            End If
        Catch objException As System.OutOfMemoryException
            MessageBox.Show("The selected file is not supported by this program.", "Unsupported File", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        End Try
    End Sub

    Private Sub SavePictureToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavePictureToolStripMenuItem.Click
        If (picShowPicture.Image Is Nothing) Then
            MessageBox.Show("Please choose an image first!", "Nothing to save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        If (ofdSavePicture.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            If (System.IO.File.Exists(ofdSavePicture.FileName)) Then
                System.IO.File.Delete(ofdSavePicture.FileName)
            End If
            picShowPicture.Image.Save(ofdSavePicture.FileName)
            lblStatus.Text = "An image is saved"
            Me.Text = "Image Viewer - Save Path(" & ofdSavePicture.FileName & ")"
        End If
    End Sub

    Private Sub ClearPictureToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearPictureToolStripMenuItem.Click
        Dim img As Image = picShowPicture.Image
        img = picShowPicture.Image
        picShowPicture.Image = Nothing
        If Not (img Is Nothing) Then
            img.Dispose()
            Me.Text = ""
            lblStatus.Text = "No image is loaded"
        End If
    End Sub
End Class

Try this:

Public Class ChildForm
    Dim saved As Boolean = False

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Try
            If ofdSelectPicture.ShowDialog = DialogResult.OK Then
                picShowPicture.Image = Image.FromFile(ofdSelectPicture.FileName)
                Me.Text = "Image Viewer(" & ofdSelectPicture.FileName & ")"
                lblStatus.Text = "An image is loaded"
            End If
        Catch objException As System.OutOfMemoryException
            MessageBox.Show("The selected file is not supported by this program.", "Unsupported File", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        End Try
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Dim img As Image = picShowPicture.Image
        img = picShowPicture.Image
        picShowPicture.Image = Nothing
        If Not (img Is Nothing) Then
            img.Dispose()
            Me.Text = ""
            lblStatus.Text = "No image is loaded"
        End If
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        If (picShowPicture.Image Is Nothing) Then
            MessageBox.Show("Please choose an image first!", "Nothing to save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        If (ofdSavePicture.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            If (System.IO.File.Exists(ofdSavePicture.FileName)) Then
                System.IO.File.Delete(ofdSavePicture.FileName)
            End If
            picShowPicture.Image.Save(ofdSavePicture.FileName)
            saved = True
            lblStatus.Text = "An image is saved"
            Me.Text = "Image Viewer - Save Path(" & ofdSavePicture.FileName & ")"
        End If
    End Sub

    Private Sub picShowPicture_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles picShowPicture.MouseLeave
        lblX.Text = ""
        lblY.Text = ""
    End Sub

    Private Sub picShowPicture_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picShowPicture.MouseMove
        lblX.Text = "X: " & e.X
        lblY.Text = "Y: " & e.Y
    End Sub

    Private Sub ChildForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If (saved = False) and (picShowPicture.Image IsNot Nothing) Then
            If MessageBox.Show("You've got unsaved changes. Are you sure you wish to proceed in closing this file without saving?", "Gavo® Ultimate Suite 2010", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub ChildForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblX.Text = ""
        lblY.Text = ""
    End Sub

    Private Sub OpenPictureToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenPictureToolStripMenuItem.Click
        Try
            If ofdSelectPicture.ShowDialog = DialogResult.OK Then
                picShowPicture.Image = Image.FromFile(ofdSelectPicture.FileName)
                Me.Text = "Image Viewer(" & ofdSelectPicture.FileName & ")"
                lblStatus.Text = "An image is loaded"
            End If
        Catch objException As System.OutOfMemoryException
            MessageBox.Show("The selected file is not supported by this program.", "Unsupported File", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        End Try
    End Sub

    Private Sub SavePictureToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavePictureToolStripMenuItem.Click
        If (picShowPicture.Image Is Nothing) Then
            MessageBox.Show("Please choose an image first!", "Nothing to save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        If (ofdSavePicture.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            If (System.IO.File.Exists(ofdSavePicture.FileName)) Then
                System.IO.File.Delete(ofdSavePicture.FileName)
            End If
            picShowPicture.Image.Save(ofdSavePicture.FileName)
            saved = True
            lblStatus.Text = "An image is saved"
            Me.Text = "Image Viewer - Save Path(" & ofdSavePicture.FileName & ")"
        End If
    End Sub

    Private Sub ClearPictureToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearPictureToolStripMenuItem.Click
        Dim img As Image = picShowPicture.Image
        img = picShowPicture.Image
        picShowPicture.Image = Nothing
        If Not (img Is Nothing) Then
            img.Dispose()
            Me.Text = ""
            lblStatus.Text = "No image is loaded"
        End If
    End Sub
End Class

first u have to think of a way to determine whether data is saved or not.
thn this factor can be used as a condition to determine if u want to run the above code or not.

Private Sub ChildForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
if DeterminingFactor = true then
        If picShowPicture.Image IsNot Nothing Then
            If MessageBox.Show("You've got unsaved changes. Are you sure you wish to proceed in closing this file without saving?", "Gavo® Ultimate Suite 2010", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
                e.Cancel = True
            End If
        End If
end if
    End Sub

Even though I used sknake's method, I want to thank you both guys for helping me out. Is there anything I can do for you?

Just mark the thread as solved, which you already did :)

Post back if you have any questions and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.