开发者

Deep copying of tree view nodes

开发者 https://www.devze.com 2022-12-25 09:05 出处:网络
I\'m trying to copy a treeview nodes to treenodecollection for some other processing. When i execute the treeview.nodes.clear()开发者_运维百科 in the next line, my treenodecollection is becoming null.

I'm trying to copy a treeview nodes to treenodecollection for some other processing. When i execute the treeview.nodes.clear()开发者_运维百科 in the next line, my treenodecollection is becoming null. Can you please tell me how to copy the treeview nodes to treenodecollection and keep the copies of the nodes even after calling Clear method of actual tree view nodes?

TreeNodeCollection tnc = null;
private TypeIn()
{
      tnc = treeView1.Nodes;
      treeView1.Nodes.Clear();
      //Now my tnc becomes null, but I want the tnc for future use.
}


TreeNode object is clonable with all subtree entire. Thats why you can use List which will contain root nodes with there subtrees.

List<TreeNode> tnc = null;
private TypeIn()
{
      tnc  = new List<TreeNode>();
      foreach (TreeNode n in treeView1.Nodes)
      {
          tnc.Add((TreeNode)n.Clone());
      }
      treeView1.Nodes.Clear();

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号