Treeview Collection

G

Guest

I have a Treeview control like the following:

paren1 ''(Name = P1)
Parent2 ''(Name = P2)
Child1 ''(Name = P2C1)
Child2 ''(Name = P2C2)
Parent3 ''(Name = P3)
Parent4 ''(Name = P4)
Child1 ''(Name = P4C1)
Parent5 ''(Name = P5)
Parent6 ''(Name = P6)



1. How do I loop through the tree and find the index or Name of the node
whose Name = P2C2 and then select it.

2. I want to get the index or Name whose Name = P5 and select it.
 
R

Robbe Morris [C# MVP]

Use recursion (methods that call themselves while passing
in a child object). Strip out all the junk you don't need.
CustomItem was my class stored in the .Tag object of
each node. This way, I could find nodes based on
a wide variety of criteria elsewhere.

public TreeNode SelectedTreeNode = null;

public void SelectNodeByUniqueKey(TreeView tv, string uniqueKey)
{
TreeNode node = null;
CustomItem customItem = null;

try
{
tv.SelectedNode = null;
this.SelectedTreeNode = null;
node = tv.Nodes[0];
customItem = (CustomItem)node.Tag;

if (customItem.StringID == uniqueKey)
{
tv.SelectedNode = node;
this.SelectedTreeNode = tv.SelectedNode;
return;
}

SelectNodeByUniqueKey(node, uniqueKey);
}
catch (Exception) { throw; }
}

private void SelectNodeByUniqueKey(TreeNode parentNode,
string uniqueKey)
{
TreeNode node = null;
CustomItem customItem = null;

try
{

for (int i = 0; i < parentNode.Nodes.Count; i++)
{
node = parentNode.Nodes;
customItem = (CustomItem)node.Tag;

if (customItem.StringID == uniqueKey)
{
node.TreeView.SelectedNode = node;
this.SelectedTreeNode = node.TreeView.SelectedNode;
return;
}

if (this.SelectedTreeNode == null)
{
SelectNodeByUniqueKey(node, uniqueKey);
}

}
}
catch (Exception) { throw; }
return;
}


--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp
 
G

Guest

I would appreciate if someone could provide the same code in VB.Net. Thanks

Robbe Morris said:
Use recursion (methods that call themselves while passing
in a child object). Strip out all the junk you don't need.
CustomItem was my class stored in the .Tag object of
each node. This way, I could find nodes based on
a wide variety of criteria elsewhere.

public TreeNode SelectedTreeNode = null;

public void SelectNodeByUniqueKey(TreeView tv, string uniqueKey)
{
TreeNode node = null;
CustomItem customItem = null;

try
{
tv.SelectedNode = null;
this.SelectedTreeNode = null;
node = tv.Nodes[0];
customItem = (CustomItem)node.Tag;

if (customItem.StringID == uniqueKey)
{
tv.SelectedNode = node;
this.SelectedTreeNode = tv.SelectedNode;
return;
}

SelectNodeByUniqueKey(node, uniqueKey);
}
catch (Exception) { throw; }
}

private void SelectNodeByUniqueKey(TreeNode parentNode,
string uniqueKey)
{
TreeNode node = null;
CustomItem customItem = null;

try
{

for (int i = 0; i < parentNode.Nodes.Count; i++)
{
node = parentNode.Nodes;
customItem = (CustomItem)node.Tag;

if (customItem.StringID == uniqueKey)
{
node.TreeView.SelectedNode = node;
this.SelectedTreeNode = node.TreeView.SelectedNode;
return;
}

if (this.SelectedTreeNode == null)
{
SelectNodeByUniqueKey(node, uniqueKey);
}

}
}
catch (Exception) { throw; }
return;
}


--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp





Sean said:
I have a Treeview control like the following:

paren1 ''(Name = P1)
Parent2 ''(Name = P2)
Child1 ''(Name = P2C1)
Child2 ''(Name = P2C2)
Parent3 ''(Name = P3)
Parent4 ''(Name = P4)
Child1 ''(Name = P4C1)
Parent5 ''(Name = P5)
Parent6 ''(Name = P6)



1. How do I loop through the tree and find the index or Name of the node
whose Name = P2C2 and then select it.

2. I want to get the index or Name whose Name = P5 and select it.
 
G

Guest

Robbe,
Thnaks for you reply. Even though I have converted your code to VB.Net; I am
still unclear on CustomItem.
In my Treeview; I have set the Text and the Name properties for each node.
The Name property is always unique.

Robbe Morris said:
Use recursion (methods that call themselves while passing
in a child object). Strip out all the junk you don't need.
CustomItem was my class stored in the .Tag object of
each node. This way, I could find nodes based on
a wide variety of criteria elsewhere.

public TreeNode SelectedTreeNode = null;

public void SelectNodeByUniqueKey(TreeView tv, string uniqueKey)
{
TreeNode node = null;
CustomItem customItem = null;

try
{
tv.SelectedNode = null;
this.SelectedTreeNode = null;
node = tv.Nodes[0];
customItem = (CustomItem)node.Tag;

if (customItem.StringID == uniqueKey)
{
tv.SelectedNode = node;
this.SelectedTreeNode = tv.SelectedNode;
return;
}

SelectNodeByUniqueKey(node, uniqueKey);
}
catch (Exception) { throw; }
}

private void SelectNodeByUniqueKey(TreeNode parentNode,
string uniqueKey)
{
TreeNode node = null;
CustomItem customItem = null;

try
{

for (int i = 0; i < parentNode.Nodes.Count; i++)
{
node = parentNode.Nodes;
customItem = (CustomItem)node.Tag;

if (customItem.StringID == uniqueKey)
{
node.TreeView.SelectedNode = node;
this.SelectedTreeNode = node.TreeView.SelectedNode;
return;
}

if (this.SelectedTreeNode == null)
{
SelectNodeByUniqueKey(node, uniqueKey);
}

}
}
catch (Exception) { throw; }
return;
}


--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp





Sean said:
I have a Treeview control like the following:

paren1 ''(Name = P1)
Parent2 ''(Name = P2)
Child1 ''(Name = P2C1)
Child2 ''(Name = P2C2)
Parent3 ''(Name = P3)
Parent4 ''(Name = P4)
Child1 ''(Name = P4C1)
Parent5 ''(Name = P5)
Parent6 ''(Name = P6)



1. How do I loop through the tree and find the index or Name of the node
whose Name = P2C2 and then select it.

2. I want to get the index or Name whose Name = P5 and select it.
 

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