I have many nodes in the treeview like nodes it children, children of children....
i wanted to copy those entire content and paste it in another node.
I dont want to use clone method since it affects object of original node from which it s copied.
Try using recursive like this
private void IterateTreeNodes( TreeNode originalNode, TreeNode rootNode )
{
foreach ( TreeNode childNode in originalNode.Nodes )
{
TreeNode newNode = new TreeNode( childNode.Text );
newNode.Tag = childNode.Tag;
treeView2.SelectedNode = rootNode;
treeView2.SelectedNode.Nodes.Add( newNode );
IterateTreeNodes( childNode, newNode );
}
}
// copy nodes from treeView1 to treeView2
private void button1_Click( object sender, EventArgs e )
{
foreach ( TreeNode originalNode in treeView1.Nodes )
{
TreeNode newNode = new TreeNode( originalNode.Text );
newNode.Tag = originalNode.Tag;
treeView2.Nodes.Add( newNode );
IterateTreeNodes( originalNode, newNode );
}
}
it's a code snippet from http://windowsclient.net/blogs/faqs/archive/2006/05/30/how-do-i-clone-or-copy-all-the-nodes-from-one-treeview-control-to-another.aspx
You can use any of the traversal algorithm
at each step, you can you can put the traversed node to an object and put that object in an objectList...
All the best...
Related
I've done a lot of searching, but none of the existing solutions solve my exact problem. I have a list:
Input[] inputs = new Input[]
{
new Input(1),
new Input(3,1),
new Input(19,3),
new Input(22,1),
new Input(4,1),
new Input(5,22),
};
Here is the declaration for BuildTree() which currently does not work:
public TreeNode BuildTree(IEnumerable<Input> inputs, TreeNode parentNode = null)
{
List<Input> nodes = inputs.ToList<Input>();
foreach (Input node in nodes)
{
TreeNode newNode = new TreeNode(node.Id);
if (parentNode == null)
{
parentNode = BuildTree(nodes, newNode);
}
else
{
parentNode.AddChild(newNode);
}
}
return parentNode;
}
Here is the call to BuildTree:
TreeNode rootNode = BuildTree(inputs);
So the BuildTree function has to return the root of the tree after building it. I've tried looping through the inputs. I've tried removing the first input from the list with each iteration. I can't quite figure it out. Any help would be greatly appreciated! Thank you!
You don't need recursion because you are transforming a list into a tree, not a tree into a list.
Since your input list is none of the standard tree traversals, and you did not tell us if parent nodes always come before child nodes, the easiest way we can use is a Dictionary.
public TreeNode BuildTree(IEnumerable<Input> inputs)
{
TreeNode rootNode = null;
// Build a dictionary to store each input and its node
var dict = inputs.ToDictionary(
input => input.Id,
input => new { Input = input, Node = new TreeNode(input.Id.ToString()) });
// Iterate through the nodes and build relationship among them
foreach(var value in dict.Values)
{
var input = value.Input;
if(input.ParentId != null)
{
dict[(int)input.ParentId].Node.Nodes.Add(value.Node);
}
else
{
rootNode = value.Node;
}
}
return rootNode;
}
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i = 0;
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= (nodeList.Count - 1); i++)
{
xNode = inXmlNode.ChildNodes[i];
if (null != xNode)
{
inTreeNode.ChildNodes.Add(new TreeNode(xNode.Attributes[0].Value));
tNode = inTreeNode.ChildNodes[i];
AddNode(xNode, tNode);
}
}
}
else
{
inTreeNode.Text = inXmlNode.InnerText.ToString();
}
}
But am getting only parent node and child nodes are not added . After going through various sites i learned that this is the error
inTreeNode.Nodes.Add(new TreeNode(xNode.Attributes[0].Value));
tNode = inTreeNode.Nodes[i];
but am not getting inTreeNode.Nodes option.
Thanks for Help
Nodes collection is at treeview level and try to look the implementation here
I tride it in this way,
private void btnFind_Click(object sender, EventArgs
{
for (int i = 0; i < treeView1.Nodes.Count - 1; i++)
{
MessageBox.Show(i.ToString());
treeView1.Nodes[i].BackColor = Color.Empty;
}
var result = from TreeNode node in treeView1.Nodes
where node.Text.Contains( Convert.ToString(txtFind.Text))
select node.Index;
foreach (int search in result)
{
treeView1.Nodes[search].BackColor = Color.Yellow;
}
}
But in this way I can find only parent nodes. Is there a proper way to do this
You can have a method to process the TreeView and then another to recursively call the child nodes. This will load _matchingNodes with all of the nodes that match your text.
Private List<TreeNode> _matchingNodes;
// Process the TreeView.
private void ProcessTreeView(TreeView treeView, String FindText)
{
_matchingNodes = new List<TreeNode>();
// Process each node recursively.
foreach (TreeNode n in treeView.Nodes)
{
if(n.Text.Contains(FindText))
_matchingNodes.Add(n);
ProcessRecursive(n, FindText);
}
}
private void ProcessRecursive(TreeNode treeNode, String FindText)
{
// Process each node recursively.
foreach (TreeNode n in treeNode.Nodes)
{
if(n.Text.Contains(FindText))
_matchingNodes.Add(n);
ProcessRecursive(n, FindText);
}
}
private void btnFind_Click(object sender, EventArgs e)
{
CallRecursive(treeView1);
}
private void PrintRecursive(TreeNode treeNode)
{
if (treeNode.Text.Contains(txtFind.Text.ToString()))
{
//MessageBox.Show(treeNode.Text);
treeNode.BackColor = Color.Blue;
}
else
{
treeNode.BackColor = Color.Empty;
}
// Print each node recursively.
foreach (TreeNode tn in treeNode.Nodes)
{
PrintRecursive(tn);
}
}
// Call the procedure using the TreeView.
private void CallRecursive(TreeView treeView)
{
// Print each node recursively.
TreeNodeCollection nodes = treeView.Nodes;
foreach (TreeNode n in nodes)
{
PrintRecursive(n);
}
}
I solved It Like this and it works as expected.
TreeView.nodes.find(nodeName,1)
The numeral 1 specifies to look at all child nodes too. A 0 means say to not include children. Only tested in Powershell.
Perhaps not so helpful for searching the text of the nodes but hopefully you can obtain the node name.
I have a TreeView in my Windows application. Tn this TreeView, the user can add some root nodes and also some sub nodes for these root nodes and also some sub nodes for these sub nodes and so on ...
For example:
Root1
A
B
C
D
E
Root2
F
G
.
.
.
Now my question is that if I am at node 'E' what is the best way to find its first root node ('Root1')?
Here is a little method for you:
private TreeNode FindRootNode(TreeNode treeNode)
{
while (treeNode.Parent != null)
{
treeNode = treeNode.Parent;
}
return treeNode;
}
you can call in your code like this:
var rootNode = FindRootNode(currentTreeNode);
public TreeNode RootTreeNode(TreeNode n) { while (n.Level > 0) { n = n.Parent; } return n; }
Example to get root treenode:
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
var node = (e == null ? ((System.Windows.Forms.TreeView)sender).SelectedNode : e.Node);
var rootNode = RootTreeNode(node);
}
Enjoy
I have a problem that I cannot seem to solve.
I am building a TreeView dynamically and I have an ordered list. I want the TreeView to build in such a way:
Node1
_Node2
__ Node3
__ _Node..N
My code is as follows:
TreeNode tn = new TreeNode();
for (int i = 0; i < EmployeesReportingLine.Count; i++ )
{
Employee ep = EmployeesReportingLine[i];
while (tn.ChildNodes.Count > 0)
tn = tn.ChildNodes[0];
TreeNode temp = new TreeNode(ep.FullName);
if (i > 0)
tn.ChildNodes.Add(temp);
else
tn = temp;
}
TreeView1.Nodes.Add(tn);
I have made several other attempts at using recursive functions but the snippet above was my best attempt.
Thanks in advance.
private void addNode(TreeNodeCollection nodes, TreeNode newnode) {
if (nodes.Count == 0) nodes.Add(newnode);
else addNode(nodes[0].Nodes, newnode);
}
Or:
private void addNode2(TreeNode start, TreeNode newnode) {
if (start.Nodes.Count == 0) start.Nodes.Add(newnode);
else addNode2(start.Nodes[0], newnode);
}