Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Recently i have been working on a windows application that contains some tree nodes that gets generated by the wizards, and the wizards contains some data that is related to the nodes.Each treenode name gets renamed by the wizard name thus forming a hierarchy of nodes.

Here are the nodes that are being generated dynamically by the wizards on the front:NODE.png - Google Drive[^]

These nodes also contains data that are being dumped into database and when right clicked on any of the nodes fetches their properties like this one:WIZARD.png - Google Drive[^].

I am able to save it using my own file extension but when i try to open that custom extension saved file i only am able to retrieve the node and am not able to view their saved data or the node properties[Like right clicking the node and viewing the saved data from the properties using the context menu strip].Context menustrip doesn't open up for the opened files.

Can anyone help me suggest something that might fetch me all the data stored on the nodes that has been dumped into the database.

Following is the code that i hav used for saving the file:

SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
            if (SaveFileDialog1.CheckPathExists == false)
            {
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.InitialDirectory = @"C:\";
                saveFileDialog1.Title = "Save Vaar Files";
                // saveFileDialog1.CheckFileExists = true;
                saveFileDialog1.CheckPathExists = true;
                saveFileDialog1.DefaultExt = "vaar";
                saveFileDialog1.Filter = "vaar file (*.vaar)|*.vaar*";
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.AddExtension = true;
                // saveFileDialog1.RestoreDirectory = true;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    pathh = saveFileDialog1.FileName.ToString();
                    saveTree(treeView1, pathh);
                }

            }

            else
            {
                saveTree(treeView1, pathh);
            }


And following is code for opening the saved file:

treeView1.Nodes.Clear();
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = @"C:\";
            openFileDialog1.Title = "Browse Text Files";

            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;

            openFileDialog1.DefaultExt = "txt";
            openFileDialog1.Filter = "vaar file (*.vaar)|*.vaar|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            openFileDialog1.ReadOnlyChecked = true;
            openFileDialog1.ShowReadOnly = true;
            string pathh = "";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pathh = openFileDialog1.FileName.ToString();
                loadTree(treeView1, openFileDialog1.FileName.ToString());
            }



After opening the saved file i am not able to open the contextmenustrip by right clicking the nodes just like the way i was able to right click on the nodes when creating them and viewing their properties...Anyhelp would be greatly appreciated..:)

What I have tried:

I have tried searching everywhere for this problem and also posted this on various forums but couldnt find any help
Posted
Comments
Richard MacCutchan 4-May-17 3:29am    
All you have shown is the code for SaveFileDialog and OpenFileDialog , neither of which have anything to do with writing or reading the data to a file.
Member 12856607 4-May-17 3:48am    
Here are the two methods that save and load the files:

Saving the tree:
saveTree(TreeView tree, string filename)
{
ArrayList al = new ArrayList();
foreach (TreeNode tn in tree.Nodes)
{
//Save each RootNode in TreeView ...
al.Add(tn);
}

// create file
Stream file = File.Open(filename, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
try
{
// Serializing Arrays
bf.Serialize(file, al);
}
catch (System.Runtime.Serialization.SerializationException e)
{
MessageBox.Show("Serialization failed : {0}", e.Message);
return -1; // ERROR
}

// Close the file
file.Close();

return 0;
}


Loading the tree:
loadTree(TreeView tree, string filename)
{
if (File.Exists(filename))
{
// open file
Stream file = File.Open(filename, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
// Object var. init.
object obj = null;
try
{
//Disable data from the file
obj = bf.Deserialize(file);
}
catch (System.Runtime.Serialization.SerializationException e)
{
MessageBox.Show("De-Serialization failed : {0}", e.Message);
return -1;
}
// Close the file
file.Close();
Create new array
ArrayList nodeList = obj as ArrayList;
// load Root-Nodes
foreach (TreeNode node in nodeList)
{
tree.Nodes.Add(node);
}
return 0;

}
else return -2; // File does not exist

}
Richard MacCutchan 4-May-17 4:27am    
It's difficult to see what is going wrong, but I wonder if it is because you create the ArrayList from the Tree. Why not serialize the TreeNodes directly?
Member 12856607 5-May-17 1:48am    
how do i serialize the treenode directly?
Richard MacCutchan 5-May-17 2:43am    
Look at the Serialize method in the documentation.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900