469,643 Members | 2,064 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

adding subnodes to a tree recursively

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
Nov 18 '05 #1
10 1779
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

Nov 18 '05 #2
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


Nov 18 '05 #3
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

Nov 18 '05 #4
Thx but i'm using the Treeview WebControl and i'm just loading my XML
the TreeNodeSrc= "Xmlfile"
Can i add the code u adviced? And where?

"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:2E**********************************@microsof t.com...
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


Nov 18 '05 #5
Hi Patrick

I'm also using the WebControl and it works fine. Just add the function to
your page class in CodeBehind and call it from your Page_Load event.
Obviously it's C#, so if you're using VB you'll have to rewrite accordingly,
but haven't got VB.net, sorry!

HTH
"Patrick.O.Ige" wrote:
Thx but i'm using the Treeview WebControl and i'm just loading my XML
the TreeNodeSrc= "Xmlfile"
Can i add the code u adviced? And where?

"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:2E**********************************@microsof t.com...
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


Nov 18 '05 #6
Can u give me an example of the code behind and
How i should call it from Page_load.
Thx
"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:88**********************************@microsof t.com...
Hi Patrick

I'm also using the WebControl and it works fine. Just add the function to
your page class in CodeBehind and call it from your Page_Load event.
Obviously it's C#, so if you're using VB you'll have to rewrite accordingly, but haven't got VB.net, sorry!

HTH
"Patrick.O.Ige" wrote:
Thx but i'm using the Treeview WebControl and i'm just loading my XML
the TreeNodeSrc= "Xmlfile"
Can i add the code u adviced? And where?

"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:2E**********************************@microsof t.com...
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
>
>
>


Nov 18 '05 #7
Hi Patrick,

Okay, basically you need to make sure all your IE Control stuff is added to
your form (you say you're using the tree so I suspect you've got that bit).

Your class for your webform *should* look something like this...

namespace yourNamespace
{
public class yourpage : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView yourtree;

private void Page_Load(object Sender, System.EventArgs e)
{
// page laod stuff here

// here we expand the tree
ExpandAll()
}

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

You need to make sure you reference Microsoft.Web.UI.WebControls at the top
of your page, like so...

using Microsoft.Web.UI.WebControls;

Hope this helps. Further information can be found by doing a search for "IE
Controls TreeView" on msdn.microsoft.com - and there's a walkthrough here...

http://msdn.microsoft.com/library/de...webcontrol.asp

HTH
Dan

"Patrick.O.Ige" wrote:
Can u give me an example of the code behind and
How i should call it from Page_load.
Thx
"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:88**********************************@microsof t.com...
Hi Patrick

I'm also using the WebControl and it works fine. Just add the function to
your page class in CodeBehind and call it from your Page_Load event.
Obviously it's C#, so if you're using VB you'll have to rewrite

accordingly,
but haven't got VB.net, sorry!

HTH
"Patrick.O.Ige" wrote:
Thx but i'm using the Treeview WebControl and i'm just loading my XML
the TreeNodeSrc= "Xmlfile"
Can i add the code u adviced? And where?

"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:2E**********************************@microsof t.com...
> 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
> >
> >
> >


Nov 18 '05 #8
Thx mate!!

"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:61**********************************@microsof t.com...
Hi Patrick,

Okay, basically you need to make sure all your IE Control stuff is added to your form (you say you're using the tree so I suspect you've got that bit).
Your class for your webform *should* look something like this...

namespace yourNamespace
{
public class yourpage : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView yourtree;

private void Page_Load(object Sender, System.EventArgs e)
{
// page laod stuff here

// here we expand the tree
ExpandAll()
}

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

You need to make sure you reference Microsoft.Web.UI.WebControls at the top of your page, like so...

using Microsoft.Web.UI.WebControls;

Hope this helps. Further information can be found by doing a search for "IE Controls TreeView" on msdn.microsoft.com - and there's a walkthrough here...
http://msdn.microsoft.com/library/de...us/dnaspp/html
/aspnet-usingtreeviewiewebcontrol.asp
HTH
Dan

"Patrick.O.Ige" wrote:
Can u give me an example of the code behind and
How i should call it from Page_load.
Thx
"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:88**********************************@microsof t.com...
Hi Patrick

I'm also using the WebControl and it works fine. Just add the function to your page class in CodeBehind and call it from your Page_Load event.
Obviously it's C#, so if you're using VB you'll have to rewrite

accordingly,
but haven't got VB.net, sorry!

HTH
"Patrick.O.Ige" wrote:

> Thx but i'm using the Treeview WebControl and i'm just loading my XML > the TreeNodeSrc= "Xmlfile"
> Can i add the code u adviced? And where?
>
>
>
> "Dan Nash" <da*@musoswire.co.uk> wrote in message
> news:2E**********************************@microsof t.com...
> > 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
> > >
> > >
> > >
>
>
>


Nov 18 '05 #9
No probs mate, hope it helped. Any probs just post again or email me.

Cheers

Dan

"Patrick.O.Ige" wrote:
Thx mate!!

"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:61**********************************@microsof t.com...
Hi Patrick,

Okay, basically you need to make sure all your IE Control stuff is added

to
your form (you say you're using the tree so I suspect you've got that

bit).

Your class for your webform *should* look something like this...

namespace yourNamespace
{
public class yourpage : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView yourtree;

private void Page_Load(object Sender, System.EventArgs e)
{
// page laod stuff here

// here we expand the tree
ExpandAll()
}

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

You need to make sure you reference Microsoft.Web.UI.WebControls at the

top
of your page, like so...

using Microsoft.Web.UI.WebControls;

Hope this helps. Further information can be found by doing a search for

"IE
Controls TreeView" on msdn.microsoft.com - and there's a walkthrough

here...

http://msdn.microsoft.com/library/de...us/dnaspp/html
/aspnet-usingtreeviewiewebcontrol.asp

HTH
Dan

"Patrick.O.Ige" wrote:
Can u give me an example of the code behind and
How i should call it from Page_load.
Thx
"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:88**********************************@microsof t.com...
> Hi Patrick
>
> I'm also using the WebControl and it works fine. Just add the function to > your page class in CodeBehind and call it from your Page_Load event.
> Obviously it's C#, so if you're using VB you'll have to rewrite
accordingly,
> but haven't got VB.net, sorry!
>
> HTH
>
>
> "Patrick.O.Ige" wrote:
>
> > Thx but i'm using the Treeview WebControl and i'm just loading my XML > > the TreeNodeSrc= "Xmlfile"
> > Can i add the code u adviced? And where?
> >
> >
> >
> > "Dan Nash" <da*@musoswire.co.uk> wrote in message
> > news:2E**********************************@microsof t.com...
> > > 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
> > > >
> > > >
> > > >
> >
> >
> >


Nov 18 '05 #10
Hi Dan i'm getting error:-
'AddressOf' operand must be the name of a method; no parentheses are needed.
I use VB.Net and i changed the C# code from:-
private void ExpandAll()
{
for(int i=0; i < yourTree.Nodes.Count; i++)
{
yourTree.Nodes[i].Expanded = true;
}
}
TO

Public Sub ExpandAll()
Dim i As Integer
For i = 0 To tvFamilyTree.Nodes.Count - 1 Step i + 1
tvFamilyTree.Nodes(i).Expanded = True
Next
End Sub

And called ExapndAll in page load
And on my ASP.NET page called it like this:-
<asp:Button BorderColor=#009966 Runat=server
OnClick="ExpandAll()"></asp:Button>

Can u telling me what 'm doing wrong!!
Thx

"Dan Nash" wrote:
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

Nov 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Kristofer Andersson | last post: by
7 posts views Thread by pembed2003 | last post: by
25 posts views Thread by prabhat143 | last post: by
1 post views Thread by Johan Åhlén | last post: by
2 posts views Thread by graphicsxp | last post: by
2 posts views Thread by aemado | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.