Reputation:
I have the following two methods which loop either a TreeView or TreeNode which I want to combine into a single method. Instead of passing the TreeView/TreeNode I could pass an Object and check the type but wanted to know if there was better way?
private TreeNode SearchNode(string nodetext, TreeNode node)
{
foreach (TreeNode nd in node.Nodes)
{
if (nd.Text == nodetext)
{
return nd;
}
}
return null;
}
private TreeNode SearchParentNode(string nodetext, TreeView trv)
{
foreach (TreeNode node in trv.Nodes)
{
if (node.Text == nodetext)
{
return node;
}
}
return null;
}
Upvotes: 3
Views: 1523
Reputation: 3299
private TreeNode SearchNode(string nodetext, object obj)
{
if(obj.GetType() Is TreeView)
{
t = (TreeView)obj;
foreach (TreeView tv in obj.Nodes)
{
if (tv.Text == nodetext)
{
return nd;
}
}
}
else
{
n = (TreeNode)obj;
foreach (TreeNode nd in obj.Nodes)
{
if (nd.Text == nodetext)
{
return nd;
}
}
}
return null;
}
Upvotes: 0
Reputation: 941585
Data structures that resemble a tree are best searched using a recursive algorithm. Which could look like:
public static TreeNode SearchNode(string tofind, TreeNodeCollection nodes) {
foreach (TreeNode node in nodes) {
if (node.Text == tofind) return node;
var nested = SearchNode(tofind, node.Nodes);
if (nested != null) return nested;
}
return null;
}
Sample usage:
var node = SearchNode("foo", treeView1.Nodes);
if (node != null) {
node.Expand();
node.EnsureVisible();
}
Note how this works for any TreeView, not just one that's limited to a single parent-child relationship. It is a depth-first search, locating the first string in the order the user sees them on the screen.
Upvotes: 2
Reputation: 66449
You want a single method that can search for a value in a NodesCollection
, so just extract the contents of both methods into a single, third method that accepts a NodesCollection
:
private TreeNode SearchNodes(string nodeText, TreeNodesCollection nodes)
{
foreach (TreeNode node in nodes)
{
if (node.Text == nodeText)
{
return node;
}
}
return null;
}
And pass it whatever collection you want to search:
return SearchNodes(nodetext, node.Nodes);
return SearchNodes(nodetext, trv.Nodes);
Also, you can reduce the SearchNodes
method to a single line using LINQ:
private TreeNode SearchNodes(string nodeText, TreeNodesCollection nodes)
{
return nodes.Cast<TreeNode>().FirstOrDefault(n => n.Text == nodeText);
}
Upvotes: 2