TreeViews

G

Guest

Hi to all,

When i use the treeview and i add nodes to it, i have the problem of the
depth. I try to explain myself.

depending of the number of nodes i add or the place where i add it, i have
to use the treeview following this sentences:

treeview1.Nodes.Add
or
treeview1.Nodes[x].Nodes.Add
or
treeview1.nodes[x].Nodes[y].Nodes.Add().
and so on...

how could programatically add nodes only in one method with one sentence? my
problem is that i dont compose this treeview from a query from database. i
want to generate each node when a event is fired in a class that i have. I
want to subscribe to this event and when the event is fired use only a
sentence.
 
B

Bry

Josema said:
Hi to all,

When i use the treeview and i add nodes to it, i have the problem of the
depth. I try to explain myself.

depending of the number of nodes i add or the place where i add it, i have
to use the treeview following this sentences:

treeview1.Nodes.Add
or
treeview1.Nodes[x].Nodes.Add
or
treeview1.nodes[x].Nodes[y].Nodes.Add().
and so on...

how could programatically add nodes only in one method with one sentence? my
problem is that i dont compose this treeview from a query from database. i
want to generate each node when a event is fired in a class that i have. I
want to subscribe to this event and when the event is fired use only a
sentence.

Could you use recursion?

I have a C++ CLR class (which I've roughly translated to C# below
without testing) that uses recursion to populate a treeview with the OU
structure of an AD domain

public void PopulateADTreeViewRecurse(TreeNode Node, DirectorySearcher
searcher)
{
SearchResultCollection results = searcer.FindAll();
foreach (SearchResult result in results)
{
DirectoryEntry de = result.GetDirectoryEntry();
TreeNode newNode = new
TreeNode(de.Properties["Name"][0].ToString(), 1, 2);
node.Nodes.Add(newNode);
newNode.Tag = de;

DirectorySearcher newSearcher = new DirectorySearcher(de,
"(objectClass=organizationalUnit)");
newSearcher.SearchScope = SearchScope.OneLevel;

PopluateADTreeViewRecurse(newNode, newSearcher);
}
}

I would call this class with the following code

DirectoryEntry rootDSE = new DirectoryEntry("LDAP://rootDSE");
string path = "LDAP://" +
rootDSE.Properties["defaultNamingContext"][0].ToString();
string nodeText =
rootDSE.Properties["defaultNamingContext"][0].ToString() + " [" +
rootDSE.Properties["dnsHostName"][0].ToString() + "]";
// The following treeView has already been created by the form designer
treeView.Nodes.Clear();
TreeNode node = new TreeNode(nodeText, 0, 0);

DirectoryEntry directoryRoot = new DirectoryEntry(path);
DirectorySearcher searcher = new DirectorySearcher(directoryRoot,
"(objectClass=organizationUnit)");
searcher.SearchScope = SearchScope.OneLevel;
PopulateADTreeViewRecurse(node, searcher);
treeView.Nodes.Add(node);

Hope that is of some help.
 
R

rhaazy

There is a way to return what "level" or "depth" of the treeview you
are in.

int treelevel = 0;
TreeNode tnTemp = treeView1.SelectedNode;
while(tnTemp.Parent != null)
{
treelevel++;
tnTemp = (TreeNode)tnTemp.Parent;
}
Msg(treelevel.ToString()); - will be the current level 0 being root.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top