martini1
May 12th, 2008, 10:03 AM
I'm using an arraylist to store an array like...
files.Add(new StoredFile(nodeTitle, nodeName, parentNode));
I'm trying to add each element of the arrayList to the treeview using a loop.
foreach (StoredFile f in files)
{
if (f.gsParentNode.Equals(mainNode.Name))
child = addTreeNode(child, mainNode, f);
MessageBox.Show(child.Name.ToString())
...
private TreeNode addTreeNode(TreeNode newNode, TreeNode rootNode, StoredFile f)
{
newNode = new TreeNode();
newNode.Name = f.gsFileName;
newNode.Text = f.gsTitle;
rootNode.Nodes.Add(newNode);
return newNode;
}
If I'm adding child nodes to mainNode from the array list. Is there a way of looping through all child nodes to check whether any of them are a parent of another node, which is to be added?
theycallmeloopy
May 12th, 2008, 07:46 PM
Here is an example of a Windows Form that does what you're asking. This is VS 2005, hence the partial class.
I created a StoredFile class just so the example can run on its own and you can plug parts of this example directly into your own code.
You'll need to add a TreeView named treeView1. (Default names are used here for simplicity.)
Anonymous methods rule. :)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<StoredFile> files = new List<StoredFile>();
files.AddRange(new StoredFile[]
{
new StoredFile("fruits", "fruits.txt", ""),
new StoredFile("red fruits", "redfruits.txt", "fruits.txt"),
new StoredFile("cherry", "cherry.txt", "redfruits.txt"),
new StoredFile("raspberry", "raspberry.txt", "redfruits.txt"),
new StoredFile("apple", "apple.txt", "redfruits.txt"),
new StoredFile("yellow fruits", "yellowfruits.txt", "fruits.txt"),
new StoredFile("lemon", "lemon.txt", "yellowfruits.txt"),
new StoredFile("banana", "banana.txt", "yellowfruits.txt"),
new StoredFile("vegetables", "vegetables.txt", ""),
new StoredFile("green vegetables", "greenvegetables.txt", "vegetables.txt"),
new StoredFile("spinach", "spinach.txt", "greenvegetables.txt"),
new StoredFile("broccoli", "broccoli.txt", "greenvegetables.txt"),
new StoredFile("zucchini", "zucchini.txt", "greenvegetables.txt")
});
foreach (StoredFile f in files.FindAll(delegate(StoredFile sf) { return sf.gsParentNode == ""; }))
{
treeView1.Nodes.Add(f.gsFileName, f.gsTitle);
}
foreach (TreeNode n in treeView1.Nodes)
{
AddNodesRecursive(files, n);
}
}
private void AddNodesRecursive(List<StoredFile> files, TreeNode n)
{
foreach (StoredFile child in files.FindAll(delegate(StoredFile sf) { return sf.gsParentNode == n.Name; }))
{
AddNodesRecursive(files, n.Nodes.Add(child.gsFileName, child.gsTitle));
}
}
}
class StoredFile
{
public string gsTitle;
public string gsFileName;
public string gsParentNode;
public StoredFile(string title, string fileName, string parentNode)
{
gsTitle = title;
gsFileName = fileName;
gsParentNode = parentNode;
}
}
}