Problem Creating TreeView
-
I am creating a TreeView which lists all the directories and ".xls" in a given directory I have created my own version of TreeView and TreeNode, just to allow my TreeNodes to have some extra information (path of the file or the directory) The problem i am facing is once done the processing the Tree View does not show mw the nodes in the Tree. if i check the count it is OK (same no of files and directories i have) but they don't show up on thew form. So i am providing my code in a hope that some1 will have an idea what i am doing wrong. The first part is the method that create the treeview(my version), and tries to copy the nodes from it to the TreeView (.net) .
private void createTree() { g_ReportsFolder = Directory.GetCurrentDirectory() + "\\Reports2"; ReportTree rtree = new ReportTree(g_ReportsFolder); foreach (ReportNode rNode in rtree.Nodes) { this.tvReports2.Nodes.Add(rNode); } }
Note: If I use
this.tvReports2.Nodes.Add(rNode.Text);
instead ofthis.tvReports2.Nodes.Add(rNode);
I can view the nodes in the treeview This is the TreeView class that creates the list of nodes by using a recursive function createTree(DirectoryInfo p_Directory)public class ReportTree : TreeView { private string ReportsFolder = ""; private DirectoryInfo Directory = null; public ReportTree(string p_ReportsFolder) { ReportsFolder = p_ReportsFolder; Directory = new DirectoryInfo(ReportsFolder); createTree(Directory); string s = this.Name; } private void createTree(DirectoryInfo p_Directory) { DirectoryInfo l_DirctoryInfo = null; //Lookout for the children of the Directory foreach (FileSystemInfo Report in p_Directory.GetFileSystemInfos()) { if (isDirectory(Report)) { createDirectoryNode(Report); l_DirctoryInfo = new DirectoryInfo(Report.FullName); createTree(l_DirctoryInfo); } else if (isXLSFile(Report)) { createFileNode(Report); } else { continue; } } } private bool isDirectory(FileSystemInfo p_FSI)
-
I am creating a TreeView which lists all the directories and ".xls" in a given directory I have created my own version of TreeView and TreeNode, just to allow my TreeNodes to have some extra information (path of the file or the directory) The problem i am facing is once done the processing the Tree View does not show mw the nodes in the Tree. if i check the count it is OK (same no of files and directories i have) but they don't show up on thew form. So i am providing my code in a hope that some1 will have an idea what i am doing wrong. The first part is the method that create the treeview(my version), and tries to copy the nodes from it to the TreeView (.net) .
private void createTree() { g_ReportsFolder = Directory.GetCurrentDirectory() + "\\Reports2"; ReportTree rtree = new ReportTree(g_ReportsFolder); foreach (ReportNode rNode in rtree.Nodes) { this.tvReports2.Nodes.Add(rNode); } }
Note: If I use
this.tvReports2.Nodes.Add(rNode.Text);
instead ofthis.tvReports2.Nodes.Add(rNode);
I can view the nodes in the treeview This is the TreeView class that creates the list of nodes by using a recursive function createTree(DirectoryInfo p_Directory)public class ReportTree : TreeView { private string ReportsFolder = ""; private DirectoryInfo Directory = null; public ReportTree(string p_ReportsFolder) { ReportsFolder = p_ReportsFolder; Directory = new DirectoryInfo(ReportsFolder); createTree(Directory); string s = this.Name; } private void createTree(DirectoryInfo p_Directory) { DirectoryInfo l_DirctoryInfo = null; //Lookout for the children of the Directory foreach (FileSystemInfo Report in p_Directory.GetFileSystemInfos()) { if (isDirectory(Report)) { createDirectoryNode(Report); l_DirctoryInfo = new DirectoryInfo(Report.FullName); createTree(l_DirctoryInfo); } else if (isXLSFile(Report)) { createFileNode(Report); } else { continue; } } } private bool isDirectory(FileSystemInfo p_FSI)
The difference between the two overloads is that the string version creates a new node; whereas the node overload just adds it to the collection. You should be getting an ArgumentException that indicates you cannot add the node in more than one place. The solution for you is to clone the node. You'll need to override the Clone method from the TreeNode base class and make sure your properties are copied. I also have a design recommendation for you. The ReportTree is not necessary. It appears that all of your implementation is related to creation. That is, you're not really maintaining any state in it. The Builder[^] pattern would work well for you. You'd still want the concrete tree node you created.