Answer the question
In order to leave comments, you need to log in
Where is TreeView tree data stored in Visual Studio and can the entire tree be serialized?
Good afternoon, dear .net gurus,
I'm trying to make a two-level list based on TreeView (WinForms). Through the interface of the form designer, you can edit the nodes of the tree, but I could not figure out how to access them programmatically. In addition, after editing in the application form, I would like to save the entire list of nodes to a file (database). Do visual components, and specifically TreeView, have a built-in ability to save the state of an object to external storage, or what method can be used to do this?
Answer the question
In order to leave comments, you need to log in
Nodes are accessed through Nodes. You can serialize. TreeView does not have a built-in load / save function.
I use these functions for this:
public void SaveTree(TreeView tree, string filename)
{
using (Stream file = File.Open(filename, FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(file, tree.Nodes.Cast<TreeNode>().ToList());
}
}
public void LoadTree(TreeView tree, string filename)
{
using (Stream file = File.Open(filename, FileMode.Open))
{
BinaryFormatter bf = new BinaryFormatter();
IEnumerable<TreeNode> obj = (IEnumerable<TreeNode>)bf.Deserialize(file);
if (obj==null )
{
obj = new TreeNode[0];
}
if (obj.Count()>0)
{
TreeNode[] nodeList = (obj as IEnumerable<TreeNode>).ToArray();
tree.Nodes.AddRange(nodeList);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question