Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Saving Tree Structure using Serialization

, 22 Jun 2009
This article demonstrates editing a tree structure and saving the job using serialization.
TreeSave.zip
TreeSave
ABSTreeSaver
bin
Debug
ABSTreeSaver.dll
Properties
HostApp
bin
Debug
ABS.TreeNodeNavigator.dll
ABSTreeSaver.dll
HostApp.exe
HostApp.vshost.exe
tree1.aks
HostApp.suo
Properties
Settings.settings
Rectangle Blue.ico
TreeReader.suo
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ABSTreeSaver;
using ABS.TreeNodeNavigator;
using System.Runtime.Serialization.Formatters.Binary;

namespace HostApp
{
    public partial class frmABSTreeView : Form
    {
        #region USER DEFINED VARIABLES
        private string fileName = "Untitled";
        #endregion
        public frmABSTreeView(string fileName)
        {
            InitializeComponent();

            if (fileName != String.Empty)
            {
                this.fileName = fileName;
                OpenFile();
            }
        }
        private void frmABSTreeView_Load(object sender, EventArgs e)
        {
        }
        #region toolstrip item clicks
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            frmEditInfo frm = new frmEditInfo();
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK)
            {                
                TreeNode STreeNode = new TreeNode();
                STreeNode.Name = (theTree.Nodes.Count + 1).ToString();
                STreeNode.Tag=frm.TagValue;
                STreeNode.Text = frm.NameValue;
                STreeNode.ImageIndex = 1;
                STreeNode.SelectedImageIndex = 1;
                STreeNode.ToolTipText = "STreeNode Tag Value: "+frm.TagValue.ToString();
                theTree.SelectedNode.Nodes.Add(STreeNode);
                STreeNode.EnsureVisible();
            }
            else
                return;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (theTree.Nodes.Count != 1)
            {
                if (theTree.SelectedNode != null)
                {
                    theTree.SelectedNode.Remove();
                }
                else
                {
                    MessageBox.Show("Select a STreeNode to remove.", "ABS Treereader", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("This is the last node and cannot be removed.", "ABS Treereader", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            frmEditInfo frm = new frmEditInfo();
            frm.NameValue = theTree.SelectedNode.Text;
            frm.TagValue = (string)theTree.SelectedNode.Tag;
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK)
            {
                theTree.SelectedNode.Tag = frm.TagValue;
                theTree.SelectedNode.Text = frm.NameValue;
            }
            else
                return;
            
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (fileName == "Untitled")
            {
                saveAsToolStripMenuItem_Click(sender, e);
            }
            else
            {
                SaveFile();
            }
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            try
            {
                this.theTree.BeginUpdate();
                Navigator trv = new Navigator();
                trv.TreeViewNode = this.theTree.SelectedNode;
                trv.MoveUp();
                this.theTree.EndUpdate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            try
            {
                this.theTree.BeginUpdate();
                Navigator trv = new Navigator();
                trv.TreeViewNode = this.theTree.SelectedNode;
                trv.MoveLeft();
                this.theTree.EndUpdate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            try
            {
                this.theTree.BeginUpdate();
                Navigator trv = new Navigator();
                trv.TreeViewNode = this.theTree.SelectedNode;
                trv.MoveRight();
                this.theTree.EndUpdate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            try
            {
                this.theTree.BeginUpdate();
                Navigator trv = new Navigator();
                trv.TreeViewNode = this.theTree.SelectedNode;
                trv.MoveDown();
                this.theTree.EndUpdate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion
        #region menu item clicks
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.theTree.Nodes.Clear();
            this.theTree.Visible = false;
        }

        private void editNodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (theTree.SelectedNode.ImageIndex == 0)
            {
                MessageBox.Show("The root node cannot be edited!", "ABS Treereader", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            frmEditInfo frm = new frmEditInfo();
            frm.NameValue = theTree.SelectedNode.Text;
            frm.TagValue = (string)theTree.SelectedNode.Tag;
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK)
            {
                theTree.SelectedNode.Tag = frm.TagValue;
                theTree.SelectedNode.Text = frm.NameValue;
                theTree.SelectedNode.Text = frm.TagValue; 
            }
            else
                return;
        }

        private void deleteNodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (theTree.Nodes.Count != 1)
            {
                if (theTree.SelectedNode != null)
                {
                    theTree.SelectedNode.Remove();
                }
                else
                {
                    MessageBox.Show("Select a STreeNode to remove.", "ABS Treereader", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("This is the last node and cannot be removed.", "ABS Treereader", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void upToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.theTree.BeginUpdate();
                Navigator trv = new Navigator();
                trv.TreeViewNode = this.theTree.SelectedNode;
                trv.MoveUp();
                this.theTree.EndUpdate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void leftToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.theTree.BeginUpdate();
                Navigator trv = new Navigator();
                trv.TreeViewNode = this.theTree.SelectedNode;
                trv.MoveLeft();
                this.theTree.EndUpdate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void rightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.theTree.BeginUpdate();
                Navigator trv = new Navigator();
                trv.TreeViewNode = this.theTree.SelectedNode;
                trv.MoveRight();
                this.theTree.EndUpdate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void downToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.theTree.BeginUpdate();
                Navigator trv = new Navigator();
                trv.TreeViewNode = this.theTree.SelectedNode;
                trv.MoveDown();
                this.theTree.EndUpdate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void addNodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmEditInfo frm = new frmEditInfo();
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK)
            {
                TreeNode node = new TreeNode();
                node.Name = (theTree.Nodes.Count + 1).ToString();
                node.Tag = frm.TagValue;
                node.Text = frm.NameValue;
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;
                node.ToolTipText = "STreeNode Tag Value: " + frm.TagValue.ToString();
                theTree.SelectedNode.Nodes.Add(node);
                node.EnsureVisible();
            }
            else
                return;
        }
        private void newToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if(theTree.Visible==false)
            {
                theTree.Visible = true;
            }
            fileName = "Untitled";
            theTree.Nodes[0].Nodes.Clear();
            SetFormTitle();
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (theTree.Visible == false)
            {
                theTree.Visible = true;
            }
            if (dlgOpenFile.ShowDialog() == DialogResult.OK)
            {
                fileName = dlgOpenFile.FileName;
                OpenFile();
                SetFormTitle();
            }
        }
        private void exitToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        #endregion
        #region 'The saving game'
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fileName == "Untitled")
            {
                saveAsToolStripMenuItem_Click(sender, e);
            }
            else
            {
                SaveFile();
            }
        }
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dlgSaveFile.ShowDialog() == DialogResult.OK)
            {
                fileName = dlgSaveFile.FileName;

                SaveFile();
                SetFormTitle();
            }
        }
        #endregion
        
        #region Function 'fnOpenFile'
        /// <summary>
        /// This function is responsible for opening the files
        /// </summary>
        protected void OpenFile()
        {
            try
            {
                BinaryFormatter bin = new BinaryFormatter();
                this.theTree.Nodes.Clear();
                FileStream fTree = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                //Fetches the data and stores in the array list
                STreeNode str = (STreeNode)bin.Deserialize(fTree);
                //Closing the file stream
                fTree.Close();
                TreeNode trParent = STROperation.fnPrepareToRead(str);
                foreach (TreeNode trn in trParent.Nodes)
                {
                    this.theTree.Nodes.Add(trn);
                }
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message, "ABS Treereader",
                   MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        #endregion
        #region function 'SaveFile'
        /// <summary>
        /// This function is responsible for saving files
        /// </summary>
        protected void SaveFile()
        {
            try
            {
                BinaryFormatter bin = new BinaryFormatter();
                STreeNode strToBeGone = STROperation.fnPrepareToWrite(this.theTree);
                //Updating in the file
                FileStream fTree = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                bin.Serialize(fTree, strToBeGone);
                fTree.Close();
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message, "Simple Editor",
                   MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        #endregion
        
        protected void SetFormTitle()
        {
            FileInfo fileinfo = new FileInfo(fileName);
            this.Text = fileinfo.Name + " - ABS Treereader";
        }

        
        

        
        


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Siddhartha S.
Software Developer (Senior)
India India
Sid loves programming and has technical experience developing desktop based solutions using C#.Net, Winforms, WPF.
He has experience of Software services, I-Banks and Product development environments.
He also has a deep understanding of Product Development Lifecycle and Agile methodology of developing softwares.
 
Besides programming he is also fond of music, photography and cooking.
He lives in Mumbai with his wife.
Follow on   LinkedIn

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 22 Jun 2009
Article Copyright 2009 by Siddhartha S.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid