Question On FormClosing messagebox keeps popup and won't exit the application

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I have a question, maybe it's a stupid question, but on the form event: FormClosing I have this code that saves a treeview to xml, when it is saved it displays a message, but when I click the messagebox OK button, the messagebox keeps popping up and won't close that form.

Here is the code I use:
VB.NET:
Private Sub AddressBook_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        'Build our List of custom objects from our TreeView's Nodes 
        Dim myNodes = BuildMyNodes(TreeView1.Nodes)
        'Serialize object to a text file. 
        Dim objStreamWriter As New StreamWriter(Application.StartupPath + "\addressbook.xml")
        Dim x As New XmlSerializer(myNodes.GetType)
        x.Serialize(objStreamWriter, myNodes)
        objStreamWriter.Close()
        MessageBox.Show("Addressbook saved!")
        Close()
    End Sub

What I even tried is putting the code between: try catch
VB.NET:
Try
            'Build our List of custom objects from our TreeView's Nodes 
            Dim myNodes = BuildMyNodes(TreeView1.Nodes)
            'Serialize object to a text file. 
            Dim objStreamWriter As New StreamWriter(Application.StartupPath + "\addressbook.xml")
            Dim x As New XmlSerializer(myNodes.GetType)
            x.Serialize(objStreamWriter, myNodes)
            objStreamWriter.Close()
            MessageBox.Show("Addressbook saved!")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Close()
but the response remains the same.

Thanks
 
Stop calling Close. The FormClosing event is raised when the form is closing, so there's no need or reason to close the form at that point because it's already closing.
 
Back
Top