TreeView Node Selection based on Fullpath

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello ,
I am working on Windows form and I have 2 Treeview controls. If I select a Node in TreeView1 then Same Node in TreeView2 should be selected(if present).

So here is how I have written a code.

tvPD1 is first Treeview and tvPD2 is second TreeView Control.
<pre style="font-family:Consolas; font-size:13; color:black; background:white <span style="color:blue private <span style="color:blue void tvPD1_AfterSelect(<span style="color:blue object sender, <span style="color:#2b91af TreeViewEventArgs e)
{
<span style="color:blue string leftTVFullPath = e.Node.FullPath;

GetNodeFromPath(tvPD2.Nodes, leftTVFullPath);
}

<span style="color:blue public <span style="color:#2b91af TreeNode GetNodeFromPath(<span style="color:#2b91af TreeNodeCollection nodes, <span style="color:blue string path)
{
<span style="color:#2b91af TreeNode foundNode = <span style="color:blue null;
<span style="color:blue foreach (<span style="color:#2b91af TreeNode tn <span style="color:blue in nodes)
{
<span style="color:blue if (tn.FullPath == path)
{
tvPD2.SelectedNode = tn;
tvPD2.SelectedNode.EnsureVisible(); <span style="color:green
tvPD2.Focus();
<span style="color:blue return tn;
}
<span style="color:blue else <span style="color:blue if (tn.Nodes.Count > 0)
{
foundNode = GetNodeFromPath(tn.Nodes, path);
}
<span style="color:blue if (foundNode != <span style="color:blue null)
<span style="color:blue return foundNode;
}
<span style="color:blue return <span style="color:blue null;
}[/code]
The main Problem with the above code is ..If there are two or more elements with the same name at the same level in the tree, this will return the first of the elements. How can we implement such function ?
Regards
Vyasa

View the full article
 
Back
Top