我试图复制整个树(确切所有节点)的一个树视图(完全)到另一个树视图使用以下代码:
TreeNodeCollection myTreeNodeCollection = treeView1.Nodes;
TreeNode[] myTreeNodeArray = new TreeNode[treeView1.Nodes.Count];
treeView1.Nodes.CopyTo(myTreeNodeArray, 0);
treeView2.Nodes.AddRange(myTreeNodeArray);
但这不允许我这样做,它要求删除源树视图中的节点或使用它克隆!我该怎么做呢?我不希望我的源树视图在这个过程中失去任何东西。
** UPDATE **好吧,伙计们,我发现了一个复杂的代码(对我来说!!),但我怎么使用它?
public static T DeepTreeCopy<T>(T obj)
{
object result = null;
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
result = (T)formatter.Deserialize(ms); ms.Close();
}
return (T)result;
}
try this
public void Copy(TreeView treeview1, TreeView treeview2)
{
TreeNode newTn;
foreach (TreeNode tn in treeview1.Nodes)
{
newTn = new TreeNode(tn.Text, tn.Value);
CopyChilds(newTn, tn);
treeview2.Nodes.Add(newTn);
}
}
public void CopyChilds(TreeNode parent, TreeNode willCopied)
{
TreeNode newTn;
foreach (TreeNode tn in willCopied.ChildNodes)
{
newTn = new TreeNode(tn.Text, tn.Value);
parent.ChildNodes.Add(newTn);
}
}
我问候像MohD的答案一样,但是使用递归来获得所有节点。(子节点的节点)
public void CopyTreeNodes(TreeView treeview1, TreeView treeview2)
{
TreeNode newTn;
foreach (TreeNode tn in treeview1.Nodes)
{
newTn = new TreeNode(tn.Text, tn.ImageIndex, tn.SelectedImageIndex);
CopyChildren(newTn, tn);
treeview2.Nodes.Add(newTn);
}
}
public void CopyChildren(TreeNode parent, TreeNode original)
{
TreeNode newTn;
foreach (TreeNode tn in original.Nodes)
{
newTn = new TreeNode(tn.Text, tn.ImageIndex, tn.SelectedImageIndex);
parent.Nodes.Add(newTn);
CopyChildren(newTn, tn);
}
}
使用node.Clone()函数也可以…
foreach (TreeNode node in treeViewSource.Nodes)
{
treeViewTarget.Nodes.Add((TreeNode)node.Clone());
}
添加根节点将有助于确保"plus - minus"功能是可见的。
TreeNode rootNode = new TreeNode("Root Node");
treeViewTarget.Nodes.Add(rootNode);
foreach (TreeNode node in treeViewSource.Nodes)
{
rootNode.Nodes.Add((TreeNode)node.Clone());
}
rootNode.Expand();
谢谢大家的回答。把所有的答案结合起来,这对我来说很有效。即使树视图有多个层次
public void CopyTreeNodes(TreeView treeview1, TreeView treeview2)
{
TreeNode newTn;
foreach (TreeNode tn in treeview1.Nodes)
{
newTn = new TreeNode(tn.Text, tn.Value);
CopyChildren(newTn, tn);
treeview2.Nodes.Add(newTn);
}
}
public void CopyChildren(TreeNode parent, TreeNode original)
{
TreeNode newTn;
foreach (TreeNode tn in original.ChildNodes)
{
newTn = new TreeNode(tn.Text, tn.Value);
parent.ChildNodes.Add(newTn);
CopyChildren(newTn, tn);
}
}
您可以在所需的级别使用
public void RootCopy(TreeView treeview1, TreeView treeview2)
{
TreeNode newNode;
foreach (TreeNode tnode in treeview1.Nodes)
{
newNode = new TreeNode(tnode.Text);
treeview2.Nodes.Add(newNode);
if (tnode.Nodes.Count != 0)
{
int _1index = tnode.Index;
ChildsCopyLevel2(_1index, treeview1, treeview2);
}
}
}
public void ChildsCopyLevel2(int index1, TreeView TV1, TreeView TV2)
{
foreach (TreeNode Tnode in TV1.Nodes[index1].Nodes)
{
string Childtext = Tnode.Text;
TV2.Nodes[index1].Nodes.Add(Childtext);
if (Tnode.Nodes.Count != 0)
{// ChildsCopyLevel3(Tnode.Nodes.Count, TV1, TV2);
int _2index = Tnode.Index;
ChildsCopyLevel3(index1, _2index, TV1, TV2);
}
}
}
public void ChildsCopyLevel3(int index1, int index2, TreeView TV1, TreeView TV2)
{
foreach (TreeNode Tnode in TV1.Nodes[index1].Nodes[index2].Nodes)
{
string Childtext = Tnode.Text;
TV2.Nodes[index1].Nodes[index2].Nodes.Add(Childtext);
}
}