Windows Develop Bookmark and Share   
 index > Windows Forms General > Problem with TreeView
 

Problem with 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 of
this.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)
{
if (p_FSI.Attributes == FileAttributes.Directory)
return true;
else
return false;
}

private bool isXLSFile(FileSystemInfo p_FSI)
{
if (p_FSI.Attributes == FileAttributes.Archive && p_FSI.Extension == ".xls")
return true;
else
return false;
}

private void createDirectoryNode(FileSystemInfo p_DirInfo)
{
string l_Text = p_DirInfo.Name;
string l_Name = p_DirInfo.Name.Replace(" ", "").Replace("'", "").Trim();
string l_Path = p_DirInfo.FullName;

//Create a new Report Node (Customization of the tree node class)
this.Nodes.Add(new ReportNode(true, l_Name, l_Text, l_Path));
}

private void createFileNode(FileSystemInfo p_FileInfo)
{
string l_Text = p_FileInfo.Name.Remove(p_FileInfo.Name.IndexOf(".xls"));
string l_Name = l_Text.Replace(" ", "").Replace("'", "").Trim();
string l_Path = p_FileInfo.FullName;

//Create a new Report Node (Customization of the tree node class)
this.Nodes.Add(new ReportNode(false, l_Name, l_Text, l_Path));
}
}
This is the class that is customization of the TreeNode. I used this to have Some Extra info like path of the file with the node
public class ReportNode : TreeNode
{
public bool isDirectory;
public string NodeName;
public string NodeText;
public string NodePath;

public ReportNode(bool p_isDirectory, string p_Name, string p_Text, string p_Path)
{
this.isDirectory = p_isDirectory;
this.NodeName = p_Name;
this.NodeText = p_Text;
this.NodePath = p_Path;

//adding data to the base (tree Node) because .NET doesn't not to pick up the attributes and override them
this.Text = this.NodeText;
this.Name = this.NodeName;
}
}
t4ure4n  Friday, March 09, 2007 12:21 PM
Took me a little but to see the problem but I think I figured it out. There are 7 overloads for the Nodes.Add() method and from what I see none of them take a ReportNode as a parameter.

Yes I think that with rNode having TreeNode as a base class it is suppose to work but it won't. So I have a fix for you.

public class rNode
{
private string rName;
private string rText;

public rNode(string name, string text)
{
rName = name;
rText = text;

}

public string Name
{
get
{
return rName;
}
}

public string Text
{
get
{
return rText;
}
}

public static explicit operator System.Windows.Forms.TreeNode(rNode node)
{
System.Windows.Forms.TreeNode tempNode = new System.Windows.Forms.TreeNode(node.Text);
tempNode.Name = node.Name;
return tempNode;
}

public static explicit operator rNode(System.Windows.Forms.TreeNode node)
{
rNode tempNode = new rNode(node.Name, node.Text);
return tempNode;
}

The use the TreeView.Nodes.Add do the following.
rNode node = new rNode("Name", "Text");
treeView1.Nodes.Add((TreeNode)node);

That should take care of your problem. Hope it helps.
theTroll
theTroll527  Friday, March 09, 2007 1:18 PM
thanks for your reply
I am going to have a look @ your suggestion and hope it works..
thanks again
t4ure4n  Friday, March 09, 2007 1:23 PM
Sure, if you have any problems let me know. For some reason I use TreeView a lot. It seems like about half of the classes I make have a function to return a TreeNode.

theTroll
theTroll527  Friday, March 09, 2007 1:51 PM
Hi there I tried your suggestion and I am currently receving this error when I compile the code

Error 1 'GTS.ReportNode.explicit operator System.Windows.Forms.TreeNode(GTS.ReportNode)': user-defined conversion to/from base class
and
Error 2 'GTS.ReportNode.explicit operator GTS.ReportNode(System.Windows.Forms.TreeNode)': user-defined conversion to/from base class Training System
The errors are related to these two methods

public static explicit operator System.Windows.Forms.TreeNode(rNode node)
{
System.Windows.Forms.TreeNode tempNode = new System.Windows.Forms.TreeNode(node.Text);
tempNode.Name = node.Name;
return tempNode;
}

public static explicit operator rNode(System.Windows.Forms.TreeNode node)
{
rNode tempNode = new rNode(node.Name, node.Text);
return tempNode;
}
t4ure4n  Friday, March 09, 2007 3:33 PM
I looked this error on MSDN and I have found this message

Error Message

'conversion routine' : user defined conversion to/from base class

User-defined conversions to values of a base class are not allowed; you do not need such an operator.

The following sample generates CS0553:

// CS0553.cs
namespace x
{
public class ii
{
}

public class a : ii
{
// delete the conversion routine to resolve CS0553
public static implicit operator ii(a aa) // CS0553
{
return new ii();
}

public static void Main()
{
}
}
}
http://msdn2.microsoft.com/en-us/library/031a701z(VS.80).aspx

t4ure4n  Friday, March 09, 2007 3:44 PM

You can use google to search for other answers

Custom Search

More Threads

• Imagelist, c# and MissingManifestResourceException
• Data Exchange (like Session, Querystring in ASP.NET)
• Office XP PIA with word
• ListView control ... Problem??
• How to only show one instance of a form
• windows messages and top-level forms
• Adding a slider
• Loading Forms Using Reflection
• Forgotten Password
• C# open and show word .doc