以递归方式将子节点添加到树中 [英] adding subnodes to a tree recursively

查看:74
本文介绍了以递归方式将子节点添加到树中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨peeps ..


我使用来自IE COntrols的TreeView控件创建一个目录

结构,并尝试递归执行。


代码有效,但我的问题是如何让子目录显示为树中的叶子而不是新的分支?




这里是代码..


private void RecurseTree(DirectoryInfo theDir,int nLevel)

{

TreeNode libraryNode = null;

DirectoryInfo [] subFolders = theDir.GetDirectories();


//填充树

for(int i = 0; i< subFolders.Length; i ++)

{

libraryNode = new TreeNode();

libraryNode.Text = subFolders [i] .Name;

treeDocuments.Nodes.Add(libraryNode);

libraryNode = null;

RecurseTree(subFolders [i],nLevel + 1);

}

}


任何帮助表示赞赏!


干杯

Dan

解决方案

你好Dan,

您是否有可能扩展所有/折叠所有

树视图?

让我知道,然后

Dan Nash < da*@musoswire.co.uk>在消息中写道

新闻:CA ********************************** @ microsof t.com ...

嗨peeps ..

我使用来自IE COntrols的TreeView控件来创建一个目录结构,并试图递归地执行它。

代码可以工作,但我的问题是如何才能使子目录
在树中显示为叶子,而不是新的分支?

这里这是代码..

private void RecurseTree(DirectoryInfo theDir,int nLevel)
{TreeNode libraryNode = null;
DirectoryInfo [] subFolders = theDir。 GetDirectories();

//填充树
for(int i = 0; i< subFolders.Length; i ++)
{
libraryNode = new TreeNode ();
libraryNode.Text = subFolders [i] .Name;
treeDocuments.Nodes.Add(libraryNode);
libraryNode = null;
RecurseTree(subFolders [i], nLevel + 1);
}

任何帮助表示赞赏!

干杯

Dan


Patrick,


试试这个..


private void ExpandAll( )

{

for(int i = 0;我< yourTree.Nodes.Count; i ++)

{

yourTree.Nodes [i] .Expanded = true;

}

}


这对我有用,虽然可能有一个简单的命令来执行此操作

(还没有找到一个!)。显然将Expanded设置为false会给你一个

CollapseAll功能:)


HTH

Dan

Patrick.O.Ige写道:

嗨丹,
你有没有办法扩展所有/折叠所有
树视图?
让我知道和thx

Dan Nash < da*@musoswire.co.uk>在消息中写道
新闻:CA ********************************** @ microsof t.com。 ..

嗨peeps ..

我使用来自IE COntrols的TreeView控件来创建一个目录结构,并尝试递归地执行。

代码有效,但我的问题是如何使子目录


在树中出现

而不是新的分支? br />
这里是代码..

private void RecurseTree(DirectoryInfo theDir,int nLevel)
{TreeNode libraryNode = null;
DirectoryInfo [] subFolders = theDir.GetDirectories();

//填充树
for(int i = 0; i< subFolders.Length; i ++)
{
libraryNode = new TreeNode();
libraryNode.Text = subFolders [i] .Name;
treeDocuments.Nodes.Add(libraryNode);
libraryNode = null;
RecurseTree(subFolders [i],nLevel + 1);
}
}

任何帮助表示赞赏!

干杯

Dan




不用担心子节点问题 - 我已经把它分类了......


tree.Nodes [i] .Nodes.Add()


Ta tho: )


Dan Nash写道:

嗨peeps ..

我使用来自IE COntrols的TreeView控件来创建目录结构,并试图递归地执行。

代码有效,但我的问题是如何让子目录在树中显示为树叶而不是新树枝?

这里这是代码..

private void RecurseTree(DirectoryInfo theDir,int nLevel)
{TreeNode libraryNode = null;
DirectoryInfo [] subFolders = theDir。 GetDirectories();

//填充树
for(int i = 0; i< subFolders.Length; i ++)
{
libraryNode = new TreeNode ();
libraryNode.Text = subFolders [i] .Name;
treeDocuments.Nodes.Add(libraryNode);
libraryNode = null;
RecurseTree(subFolders [i], nLevel + 1);
}

任何帮助表示赞赏!

干杯

Dan



Hi peeps..

Im using the TreeView control from IE COntrols to create a directory
structure, and trying to do it recursively.

The code works, but my question is simply how can I make the subdirs appear
as leaves in the Tree, rather than as new branches?

Here''s the code..

private void RecurseTree(DirectoryInfo theDir, int nLevel)
{
TreeNode libraryNode = null;
DirectoryInfo[] subFolders = theDir.GetDirectories();

// populate the tree
for (int i=0; i < subFolders.Length; i++)
{
libraryNode = new TreeNode();
libraryNode.Text = subFolders[i].Name;
treeDocuments.Nodes.Add(libraryNode);
libraryNode = null;
RecurseTree(subFolders[i], nLevel+1);
}
}

Any help appreciated!

Cheers
Dan

解决方案

Hi Dan,
Do u have a way of the possibilty of Expanding All /Collapsing All
treeviews?
let me know and thx
"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:CA**********************************@microsof t.com...

Hi peeps..

Im using the TreeView control from IE COntrols to create a directory
structure, and trying to do it recursively.

The code works, but my question is simply how can I make the subdirs appear as leaves in the Tree, rather than as new branches?

Here''s the code..

private void RecurseTree(DirectoryInfo theDir, int nLevel)
{
TreeNode libraryNode = null;
DirectoryInfo[] subFolders = theDir.GetDirectories();

// populate the tree
for (int i=0; i < subFolders.Length; i++)
{
libraryNode = new TreeNode();
libraryNode.Text = subFolders[i].Name;
treeDocuments.Nodes.Add(libraryNode);
libraryNode = null;
RecurseTree(subFolders[i], nLevel+1);
}
}

Any help appreciated!

Cheers
Dan



Patrick,

Try this..

private void ExpandAll()
{
for(int i=0; i < yourTree.Nodes.Count; i++)
{
yourTree.Nodes[i].Expanded = true;
}
}

This works for me, although there might be a simple command to do this
(haven''t found one yet!). Obviously setting Expanded to false will give you a
CollapseAll function as well :)

HTH
Dan
"Patrick.O.Ige" wrote:

Hi Dan,
Do u have a way of the possibilty of Expanding All /Collapsing All
treeviews?
let me know and thx
"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:CA**********************************@microsof t.com...

Hi peeps..

Im using the TreeView control from IE COntrols to create a directory
structure, and trying to do it recursively.

The code works, but my question is simply how can I make the subdirs


appear

as leaves in the Tree, rather than as new branches?

Here''s the code..

private void RecurseTree(DirectoryInfo theDir, int nLevel)
{
TreeNode libraryNode = null;
DirectoryInfo[] subFolders = theDir.GetDirectories();

// populate the tree
for (int i=0; i < subFolders.Length; i++)
{
libraryNode = new TreeNode();
libraryNode.Text = subFolders[i].Name;
treeDocuments.Nodes.Add(libraryNode);
libraryNode = null;
RecurseTree(subFolders[i], nLevel+1);
}
}

Any help appreciated!

Cheers
Dan




Don''t worry about the subnodes question - I''ve sorted it with...

tree.Nodes[i].Nodes.Add()

Ta tho :)

"Dan Nash" wrote:

Hi peeps..

Im using the TreeView control from IE COntrols to create a directory
structure, and trying to do it recursively.

The code works, but my question is simply how can I make the subdirs appear
as leaves in the Tree, rather than as new branches?

Here''s the code..

private void RecurseTree(DirectoryInfo theDir, int nLevel)
{
TreeNode libraryNode = null;
DirectoryInfo[] subFolders = theDir.GetDirectories();

// populate the tree
for (int i=0; i < subFolders.Length; i++)
{
libraryNode = new TreeNode();
libraryNode.Text = subFolders[i].Name;
treeDocuments.Nodes.Add(libraryNode);
libraryNode = null;
RecurseTree(subFolders[i], nLevel+1);
}
}

Any help appreciated!

Cheers
Dan



这篇关于以递归方式将子节点添加到树中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆